gpt4 book ai didi

c# MVC Dropdownlist 发布选定值

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:50 26 4
gpt4 key购买 nike

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

我的问题专门针对下拉列表:使用硬编码值:今天、月份、年份。

每当我在下拉列表中选择某些内容时,我想将值(例如“今天”)发布/提交给 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

关于c# MVC Dropdownlist 发布选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com