【发布时间】:2019-07-13 05:15:27
【问题描述】:
我已经搜索过这个,找到了几个解决方案,但似乎没有一个适合我。
我正在使用 Razor 构建 MVC 视图。 我传入的模型定义如下:
public class ActiveProxiesDTO
{
/// <summary> error message returned by the transaction </summary>
public string ErrorMsg { get; set; }
/// <summary> List of proxies returned by the transaction </summary>
public List<ActiveProxiesDetailDTO> ActiveProxies { get; set; }
}
/// <summary> A single proxy </summary>
public class ActiveProxiesDetailDTO
{
/// <summary> Proxie's PERSON id </summary>
public string ProxyId { get; set; }
/// <summary> Proxie's First Name </summary>
public string FirstName { get; set; }
/// <summary> Proxie's Last Name </summary>
public string LastName { get; set; }
/// <summary> Proxie's email address </summary>
public string EmailAddress { get; set; }
/// <summary> Does user want to reset this proxy's password?</summary>
public bool Reset { get; set; }
}
在调用视图的操作方法中,我将列表中所有项目的“重置”布尔值设置为 false。
我的观点定义如下:
@model Ellucian.Colleague.Dtos.Base.M32.ActiveProxiesDTO
@using (Html.BeginForm("PostResetProxy", "Core", FormMethod.Post, new { Area = "M32" }))
{
@Html.AntiForgeryToken()
<div class="dataentry">
Proxies:
<br />
<table>
<tr>
<th>Reset?</th> <th>Name</th> <th>Email Address(to send new password)</th>
</tr>
@for (int i = 0; i < Model.ActiveProxies.Count; i++)
{
<tr>
<td>
<!-- make sure all data bind to the model being passed to the next controller action method -->
<input type="hidden" name="ActiveProxies[@i].ProxyId" value="@Model.ActiveProxies[i].ProxyId" />
<input type="hidden" name="ActiveProxies[@i].FirstName" value="@Model.ActiveProxies[i].FirstName" />
<input type="hidden" name="ActiveProxies[@i].LastName" value="@Model.ActiveProxies[i].LastName" />
<!-- checkboxes require both a hidden and non-hidden input element -->
<input type="hidden" name="ActiveProxies[@i].Reset" value="@Model.ActiveProxies[i].Reset" />
<input type="checkbox" name="ActiveProxies[@i].Reset" value="true" />
</td>
<td>
@{ var displayName = Model.ActiveProxies[i].FirstName + " " + Model.ActiveProxies[i].LastName; }
@displayName
</td>
<td>
<input type="text" name="ActiveProxies[@i].EmailAddress" value="@Model.ActiveProxies[i].EmailAddress" />
</td>
</tr>
}
</table>
<br />
<input type="submit" />
...
目前,处理 POST 的操作方法只是:
[HttpPost]
public ActionResult PostResetProxy( ActiveProxiesDTO formData )
{
return Redirect(Url.Action("Index", "Home", new { Area = "" }));
}
我在return 语句中设置了一个断点并检查了formData 的内容。 一切看起来都是正确的:整个 ActiveProxies 列表都存在,如果我在表单上输入电子邮件地址,他们将正确地发布到列表的正确条目中。 问题是,无论我是否选中“重置”框,该值总是作为 false 传回。
我已尝试以本论坛其他地方所述的各种方式使用 Html.CheckBox 和 Html.CheckBoxFor,但没有成功。我尝试使用字符串而不是 bool 作为复选框值,但这也失败了。我没主意了。有人看到我做错了吗? -- TIA 格斯
【问题讨论】:
标签: model-view-controller checkbox collections view binding