c#MVC Dropdownlist发布选定的值

时间:2015-08-13 09:43:31

标签: c# asp.net-mvc razor drop-down-menu

首先让我这样说:我已经看过多个答案了,而我似乎无法绕过它 - 可能是我只是脑力激荡。但这是我的问题:

我的问题是关于下拉列表:使用硬编码值:今天,月,年。

每当我在下拉列表中选择某些内容时,我想发布/提交该值,例如"今天"到控制器。我该怎么做?

感谢您的时间。

@using (Html.BeginForm("DisplayDetails", "Client"))
{
    @*Start of searchBox*@
    <div id="searchBox">
        Enter client e-mail: <input id="txtUser" type="text" name="clientID" /> <input id="btnFind" type="submit" />
    </div>
    @*<--- End of searchBox --->*@

    if (Model != null)
    {
        @*Start of infobox and wrapper*@
        <div id="infoBoxWrapper">
            <div class="infoBox">
                <fieldset>
                    <legend style="padding: 0.2em 0.5em; border: 1px solid black; font-size: large;"> Client info </legend>

                    <table>
                        <tr>
                            <td> <b> First name: </b></td>
                            <td>
                                @Html.DisplayFor(m => m.FName)
                            </td>

                        </tr>
                        <tr>
                            <td> <b> Last name: </b></td>
                            <td> @Html.DisplayFor(m => m.LName) </td>
                        </tr>
                        <tr>
                            <td> <b> Phone: </b></td>
                            <td> @Html.DisplayFor(m => m.Phone)</td>
                        </tr>
                        <tr>
                            <td> <b> E-mail: </b></td>
                            <td> @Html.DisplayFor(m => m.UserID) </td>
                        </tr>
                    </table>

                </fieldset>
            </div>
            @*<--- End of infobox --->*@

            @*Start of recordsBox*@
            <div id="recordsBox">
                <fieldset>
                    <legend style="padding: 0.2em 0.5em; border: 1px solid black; font-size: large;"> Records </legend>
                    <table>
                        <tr>
                            <td> <b>Time</b></td>
                            <td> <b>Systolic</b> </td>
                            <td> <b>Diastolic</b> </td>
                            <td> <b>Pulse</b> </td>
                        </tr>
                        @for (int i = 0; i < Model.Records.Count; i++)
                        {
                            string style = null;
                            if (Model.Records[i].Score == 1) { style = "background-color:green"; }
                            else if (Model.Records[i].Score == 2) { style = "background-color:blue"; }
                            else if (Model.Records[i].Score == 3) { style = "background-color:yellow"; }
                            else if (Model.Records[i].Score == 4) { style = "background-color:orange"; }
                            else if (Model.Records[i].Score == 5) { style = "background-color:red"; }

                            <tr style="@style">
                                <td>
                                    @Model.Records[i].Time
                                </td>
                                <td>
                                    @Html.DisplayFor(m => m.Records[i].Systolic)
                                </td>
                                <td>
                                    @Html.DisplayFor(m => m.Records[i].Diastolic)
                                </td>
                                <td>
                                    @Html.DisplayFor(m => m.Records[i].Pulse)
                                </td>
                            </tr>
                        }
                    </table>
                </fieldset>
            </div>
        </div>
        @*<--- End of infobox wrapper --->*@

        @*Start of chartbox*@
        <div id="chartWrapper">
            <div id="comboBox">
                <select id="dpChoice">
                    <option value="today">Today</option>
                    <option value="month">This month</option>
                    <option value="year">This year</option>
                </select>
            </div>
            <div id="chartbox">
                @Model.Chart
            </div>
        </div>
        @*<--- End of chartbox --->*@
    }
    else
    {
        <label> Search for a client...</label>
    }
}

1 个答案:

答案 0 :(得分:2)

您的选择没有name属性,因此不会在表单数据中发送。将其更改为(比方说)

<select name="choice">
  ...
</select>

并且根据是否选择了第一个选项,它将回发choice: today,您可以通过将参数string choice添加到POST方法来访问该public class MyViewModel { public int ClientEmail{ get; set; } public string Choice { get; set; } public IEnumerable<SelectListItem> ChoiceList { get; set; } .... } 。但是我强烈建议您使用包含

的视图模型
model.ChoiceList = new List<SelectListItem>()
{
  new SelectListItem(){ Value = "today", Text = "Today" },
  new SelectListItem(){ Value = "month", Text = "This month" },
}

并在控制器中填充集合

@Html.TextBoxFor(m => m.ClientEmail)
....
@Html.DropDownListFor(m => m.Choice, Model.ChoiceList)

然后在视图中

一个未预期的错误发生在Spoon: probable cause:在停止Spoon前,请先关闭其它spoon窗口! 

然后将视图模型回发给控制器