【问题标题】:How to get checkbox input value bound to a property of a complex object list in MVC?如何获取绑定到 MVC 中复杂对象列表的属性的复选框输入值?
【发布时间】: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


    【解决方案1】:

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

    返回处理 Post 请求的方法后,我检查了 Request.Form["ActiveProxies[i].Reset"] 中的值,并注意到该值始终为 ",true"(奇怪的是它有一个 ","前缀!一个错误?)对于我有“重置?”的表的每一行选中,空字符串在我未选中的地方。

    所以我通过在我的 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 = "" }));
        }
    

    【讨论】:

      【解决方案2】:

      我刚刚发现了一个更好的解决方案。 我最初尝试了 @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 代码。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-25
        • 1970-01-01
        相关资源
        最近更新 更多