我正在编写一个 ASP.NET MVC 应用程序。我是初学者,所以我相信这真的很容易。我引用了 ASP.NET MVC - How to get checkbox values on post但代码不再可用,使其几乎无用。
这是我的观点:
@using (@Html.BeginForm("Promote", "FPL", FormMethod.Post))
{
<div data-role="fieldcontain">
<fieldset id="PromotionInfo" name="PromotionInfo">
<legend><b>@Model.itemType Promotion Details</b></legend>
<label for="TargetDate">Target Date: </label>
<input type="date" id="TargetDate" data-role="datebox" data-options='{"mode": "flipbox"}' />
<label for="EmailCC">Email cc List: </label>
<input type="email" id="EmailCC" />
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose Destination Server(s): </legend>
@foreach (DataRow server in Model.destinationServerList.Rows)
{
<label for="@server.ItemArray[0]">@server.ItemArray[1]</label>
<input type="checkbox" name="destinationServerSID" id="@server.ItemArray[0].ToString()" />
}
</fieldset>
</div>
</fieldset>
</div>
<input type="submit" value="Submit" />
}
这是我的 Controller :
public ActionResult Promote(string id)
{
//Model(item) construction occurs here
return View(item);
}
[HttpPost]
public ActionResult Promote(FormCollection collection)
{
try
{
string[] test = collection.GetValues("destinationServerSID");
}
catch (Exception ex)
{
return null;
}
}
test[] 变量包含一个 2 项数组,它们的值都为“on”,但是,我的项列表长于 2 个项。它只包含您选择的每个值的“开”,但没有“关”值。我需要复选框的实际值(id 字段)。
最佳答案
ID 不会发布到服务器。只有名称和值被发布到服务器,并且只有在复选框被选中时。
尝试设置 value
属性与 @server.ItemArray[0].ToString()
.
<input type="checkbox" name="destinationServerSID" value="@server.ItemArray[0].ToString()" />
关于asp.net-mvc - 在回发 ASP.NET MVC 上获取复选框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11618504/