如何将复选框输入值绑定到MVC中复杂对象列表的属性?

问题描述 投票:0回答:2

我已经搜索了这个,并找到了几个解决方案,但似乎没有一个对我有用。

我正在使用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; }
}

在调用视图的action方法中,我将列表中的所有项目的“Reset”bool设置为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 Gus

model-view-controller checkbox collections view binding
2个回答
0
投票

我发现了一个适合我的解决方案(它不涉及JavaScript(我还不精通)。

在返回处理Post请求的方法后,我检查了Request.Form [“ActiveProxies [i] .Reset”]中的值,并注意到该值始终为“,true”(奇怪的是它有一个“,”前缀!A bug?)表格的每一行我都有“重置?”检查,空字符串无论我在哪里取消选中它。

所以我通过在POST操作的顶部添加逻辑来纠正我的formData参数中的值,从而解决了这个问题。 (未完成的)Post方法现在如下所示

    [HttpPost]
    public ActionResult PostResetProxy( ActiveProxiesDTO formData )
    {
        for ( int i=0 ; i < formData.ActiveProxies.Count ; i++ )
        {
            string x = Request.Form[ "ActiveProxies["+i+"].Reset"];
            formData.ActiveProxies[i].Reset = x.Contains("true") ? true : false;
        }

        // for the time being, just return to the home page
        return Redirect(Url.Action("Index", "Home", new { Area = "" }));
    }

0
投票

我刚刚发现了一个更好的解决方案。我最初尝试了@ Html.CheckBox和@ Html.CheckBoxFor助手没有成功,因此放弃了使用帮助器来输入我的复选框。

令我惊讶的是,更通用的“EditorFor”助手最终发挥作用,据称那些专门用于复选框的人没有。

我替换了我原来的台词

                       <!--  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" />

单线

           @Html.EditorFor(m => Model.ActiveProxies[i].Reset)

并且...令我惊讶的是,它有效,允许我从我的POST方法中取出kluge代码。

© www.soinside.com 2019 - 2024. All rights reserved.