【发布时间】: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