从表单MVC中的select标签获取值

时间:2014-03-18 10:34:30

标签: c# asp.net-mvc forms

我的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.DropDownList,但认为这更容易..

6 个答案:

答案 0 :(得分:1)

我测试了代码,问题是form =&#34; FormCalcInput&#34; select的属性,只需删除此即可。这应该可以解决你的问题。

<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>

答案 1 :(得分:1)

FormCalcInput

中添加表单ID Ajax.BeginForm
@using (Ajax.BeginForm("ShowDetail", "Calculation", 
         new AjaxOptions {
         InsertionMode = InsertionMode.Replace,
         UpdateTargetId = "CalcDetail",LoadingElementId = "Loader" }, 
         new { id ="FormCalcInput" })) // add from id
{


}

答案 2 :(得分:0)

尝试在操作方法中使用数据绑定值:

public ActionResult ShowDetail(string pmtFreq)
{
    // ...
}

答案 3 :(得分:0)

如果您想从表单中捕获数据,我的建议是使用强类型视图模型。

例如:

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;
}

在您的视图中,在顶部添加viewmodel的声明。

答案 4 :(得分:0)

使用喜欢  var PmtFreq = Request.Form["pmtFreq"];

答案 5 :(得分:0)

从select中删除'form =“FormCalcInput”'然后就可以了。