【问题标题】:MVC model binding not working with @Html.RadioButtonFor() along with EnumsMVC 模型绑定不适用于 @Html.RadioButtonFor() 以及枚举
【发布时间】:2016-03-13 14:44:25
【问题描述】:

好的。对于这种疯狂,我感到非常沮丧。

根据我的自定义枚举,我有一个带有 4 个单选按钮的表单。

枚举看起来像这样:

public enum PhoneSelector
{
    PrivatePhone = 0,
    WorkPhone = 1,
    PrivateCellPhone = 2,
    WorkCellPhone = 3
}

我的 ViewModel 的相关部分如下所示:

public class PersonPageViewModel 
{ 
   public PersonPageForm PersonPageForm { get; set; }
}
public class PersonPageForm 
{
   public List<PhoneSelector> PhoneSelectors { get; set; }
   public PhoneSelector SelectedPhoneType { get; set; }`
}

视图模型的人口:

PersonPageForm = new PersonPageForm
{
    PhoneSelectors = Enum.GetValues(typeof(PhoneSelector)).OfType<PhoneSelector>().ToList(),
},

那么在我看来,我有以下代码:(在Html.BeginForm() 内)

@for (var i = 0; i < Model.PersonPageForm.PhoneSelectors.Count(); i++)
{
    var currentValue = Model.PersonPageForm.PhoneSelectors[i];
    <div class="row" style="@(string.IsNullOrWhiteSpace(userFields[i]) ? "display:none;" : string.Empty)">
        <div class="large-6 columns">
            @Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, currentValue, new {id = currentValue, Name = currentValue})
            @Html.LabelFor(x => @currentValue, Html.Translate("/radiobuttonlist/" + @currentValue) + " (" + @userFields[i] + ")", new {style = "font-weight: normal !important;"})
        </div>
    </div>
}

最后,控制器看起来像这样:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(PersonPageForm personPageForm, List<String> roleList, List<Int32> categoryList, String organizationType, HttpPostedFileBase userProfileImage)

这里的问题是每次提交到达我的 ActionResult 时,personPageForm.SelectedPhoneType 被设置为 PhoneSelector.PrivatePhone。 modelbinder 似乎没有得到我想要在这里做的事情。 有人可以提供有关此解决方案的一些信息以及为什么它没有按预期工作。

我也尝试了一种更简单的方法...

@Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, PhoneSelector.PrivateCellPhone, new {id = Guid.NewGuid()})
@Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, PhoneSelector.PrivatePhone, new { id = Guid.NewGuid() })
@Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, PhoneSelector.WorkCellPhone, new { id = Guid.NewGuid() })
@Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, PhoneSelector.WorkPhone, new { id = Guid.NewGuid() })

...但仍然得到相同的结果。

【问题讨论】:

    标签: asp.net-mvc enums radio-button model-binding


    【解决方案1】:

    您只需要下面的代码......框架应该为您处理其余的事情

    @Html.RadioButtonFor(x => x.PersonPageForm.SelectedPhoneType, currentValue)
    

    【讨论】:

      【解决方案2】:

      您视图中的模型是PersonPageViewModel,并且您生成的单选按钮基于该模型(即它们是&lt;input type="radio" name="PersonPageForm.SelectedPhoneType" .... /&gt;),但是您POST方法中的参数不是您的模型,它只是您模型的一些属性结果绑定失败,您的属性值是您的属性的默认值 (PrivatePhone)。

      您可以通过多种方式解决此问题。更改 POST 方法以接受您发回的模型

      public ActionResult Edit(PersonPageViewModel model)
      

      或使用[Bind] 属性的Prefix 属性从表单数据中去除"PersonPageForm" 前缀

      public ActionResult Edit([Bind(Prefix="PersonPageForm")]PersonPageForm personPageForm, List<String> roleList, ...)
      

      但是,您将其复杂化并且没有正确使用视图模型。 PersonPageForm 类是不必要的,您的视图模型应该是

      public class PersonPageViewModel
      {
          public PhoneSelector SelectedPhoneType { get; set; }
      }
      

      在视图中

      foreach (Enum item in Enum.GetValues(typeof(PhoneSelector)))
      {
        <label>
          @Html.RadioButtonFor(m => m.SelectedPhoneType, item, new { id = "" })
          <span>@item</span>
        </label>
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-14
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多