2015-12-08 59 views
-1

好吧。我對這種瘋狂感到非常沮喪。與@ Html.RadioButtonFor()一起使用的MVC模型綁定以及枚舉

我有一個4張單選按鈕的表單,基於我的自定義枚舉。

枚舉看起來是這樣的:

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

我的視圖模型的相關部分看起來是這樣的:

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。模型綁定器似乎沒有得到我想要在這裏做的。 有人可以提供有關此解決方案的一些信息,以及爲什麼它不能按預期工作。

我也試着用更簡單的方法...

@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() }) 

...但還是結束了相同的結果。

回答

0

所有你需要下面的代碼...框架應照顧其餘的爲你

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

在您的視圖模型是PersonPageViewModel,你產生的單選按鈕是基於模型(即它們是<input type="radio" name="PersonPageForm.SelectedPhoneType" .... />),但是您的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> 
}