时间:2019-05-17 标签:c#MVCDropdownlistpost选择值

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

首先让我这样说:我已经看过多个答案,但我似乎无法全神贯注 - 可能我只是脑子有问题。但我的问题是:

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

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

感谢您的宝贵时间。

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

最佳答案

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

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

并且根据是否选择了第一个选项,它将回发choice: Today,您可以通过将参数string choice添加到 POST 方法来访问它。不过,我强烈建议您使用 View 模型,包括

public class MyViewModel
{
  public int ClientEmail{ get; set; }
  public string Choice { get; set; }
  public IEnumerable<SelectListItem> ChoiceList { get; set; }
  ....
}

并填充 Controller 中的集合

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

然后在 View 中

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

然后将 View 模型回发到 Controller

关于时间:2019-05-17 标签:c#MVCDropdownlistpost选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984714/

相关文章:

jquery - 输入类型按钮/提交同时进行表单验证并通过 jquery ajax 提交?

html - 无法使用 Razor 在 foreach 中关闭标记 - ASP.NET MVC

c# - Razor 中的简单 html 与扩展方法(偏好)

CSS 条件注释不起作用(更新为 : IE specific css? )

c# - 静态函数并发 ASP.NET

c# - 将代码重构为泛型方法

asp.net-mvc - 最好的 ASP.NET MVC 教程是什么? <结束>

asp.net - 将确认或错误消息传递回 View 的好方法是什么?

c# - Task.Wait() 使我的应用程序死锁

创建新的 SqlDataAdapter 时出现 C# InvalidOperationException