从表单MVC中的select标签中获取值
本文关键字:获取 标签 select 表单 MVC 中的 | 更新日期: 2023-09-27 17:50:42
我的HTML(在一个表单内):
@using (Ajax.BeginForm("ShowDetail", "Calculation", new AjaxOptions
{
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "CalcDetail",
LoadingElementId = "Loader",
}))
{
<div class="calculateBox">
<label for="calcOption">Choose value to calculate:</label>
<select form="FormCalcInput" id="calcOption" title="Choose what value you want to calculate">
<option value="anPMT">Payment (PMT)</option>
<option value="anI">Interest (I)</option>
<option value="anFV">Future value (FV)</option>
<option value="anPV">Present value (PV)</option>
</select>
</div>
<div class="calculateBox" background-color="#777777">
@Html.Label("Present value (PV)")
@Html.TextBox("PresentValue")
@Html.Label("Future value")
@Html.TextBox("FutureValue")
@Html.Label("Interest rate")
@Html.TextBox("InterestRate") <br />
<input type="radio" name="advanceOrArrears" id="inAdvance" value="inAdvance" /> In advance<br />
<input type="radio" name="advanceOrArrears" id="inArrears" value="inArrears" /> In arrears
</div>
<div class="calculateBox">
<label for="startDate">Start date:</label>
<input type="date" id="StartDate" name="startdate" title="Choose start date for your calculation" /><br />
@Html.Label("Payment frequency")
<select form="FormCalcInput" id="PmtFreq" name="pmtFreq" title="Choose the payment frequency, for example: Every month">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Yearly">Yearly</option>
</select><br /><br />
@Html.Label("No of payment periods")
@Html.TextBox("PaymentPeriods")
@Html.Label("Date time convention")
<select form="FormCalcInput" id="anDTC" title="Choose your Date time convention">
<option value="360360">360/360</option>
<option value="365365">365/365</option>
</select><br /><br />
<input type="submit" id="CalcBtn" class="calcBtn" name="SubmitBtn" value="Calculate" title="Calculate your calculation" />
</div>
}
在我的控制器中:
[HttpPost]
public ActionResult ShowDetail(FormCollection form)
{
var PmtFreq = form["pmtFreq"];
}
为什么我的控制器中的变量不包含我的组合框中的选定值?
我试过使用@Html。下拉列表,但认为这更容易…
我已经测试了代码和问题是form="FormCalcInput"属性的选择,只是删除这个。这应该能解决你的问题。
<select id="PmtFreq" name="pmtFreq" title="Choose the payment frequency, for example: Every month">
<option value="Monthly">Monthly</option>
<option value="Quarterly">Quarterly</option>
<option value="Yearly">Yearly</option>
</select>
在Ajax.BeginForm
中添加表单id FormCalcInput
@using (Ajax.BeginForm("ShowDetail", "Calculation",
new AjaxOptions {
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "CalcDetail",LoadingElementId = "Loader" },
new { id ="FormCalcInput" })) // add from id
{
}
尝试在操作方法中使用数据绑定值:
public ActionResult ShowDetail(string pmtFreq)
{
// ...
}
如果您想从表单中捕获数据,我的建议是使用强类型视图模型。
例如:public class MyViewModel() {
public string pmtFreq {get; set;}
/* add more properties for each html input */
}
然后在控制器上:
[HttpPost]
public ActionResult ShowDetail(MyViewModel model)
{
var PmtFreq = model.pmtFreq;
}
在视图的顶部添加一个视图模型的声明。
使用likevar PmtFreq = Request.Form["pmtFreq"];
只要从select中删除'form="FormCalcInput"'就可以了