2014-01-06 55 views
0

的选择数值没有得到回发时,我组我的单选按钮,radiobuttonfor分组剃刀mvc5

因此,与此SelectedQuestionId 空白

<div class="row"> 
     <div class="col-md-4"> 
      <div class="form-group"> 

       @Html.HiddenFor(x => x.UserResponses[0].QuestionId) 
       @Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name ="selectone"}) 
       @Html.DisplayTextFor(x => x.UserResponses[0].QuestionText) 
       @Html.HiddenFor(x => x.UserResponses[0].QuestionText) 
       @Html.HiddenFor(x => x.UserResponses[0].InputType) 
       @Html.HiddenFor(x => x.UserResponses[0].Points) 

      </div> 
     </div> 

     <div class="col-md-4"> 
      <div class="form-group"> 
       <span class="text-muted">@Html.DisplayTextFor(x => x.UserResponses[0].Points)</span>&nbsp;@Html.LabelFor(x => x.UserResponses[0].Points, new { @class = "text-muted" }) 
      </div> 
     </div> 

    </div> 

    <div class="row"> 
      <div class="col-md-4"> 
       <div class="form-group"> 

        @Html.HiddenFor(x => x.UserResponses[1].QuestionId) 
        @Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"}) 
        @Html.DisplayTextFor(x => x.UserResponses[1].QuestionText) 
        @Html.HiddenFor(x => x.UserResponses[1].QuestionText) 
        @Html.HiddenFor(x => x.UserResponses[1].InputType) 
        @Html.HiddenFor(x => x.UserResponses[1].Points) 




       </div> 
      </div> 

      <div class="col-md-4"> 
       <div class="form-group"> 
        <span class="text-muted">@Html.DisplayTextFor(x => x.UserResponses[1].Points)</span>&nbsp;@Html.LabelFor(x => x.UserResponses[1].Points, new { @class = "text-muted" }) 
       </div> 
      </div> 

     </div> 

但当下我删除分组从radiobuttonfor然后一切运作良好,并SelectedQuestionId填充questionid。因此,用这些替换上面的RadioButtonFor允许在发布表单时填充SelectedQuestionId。 上面的代码是基于这个question,虽然我已经问了一个相关的问题here - 这个问题是完全分开的。

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId) 

@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId) 

回答

0

找到了解决这个相当棘手的问题。

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId) 

@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId) 

每个单选按钮上面的代码反序列化过程中SelectedQuestionId属性将被填充为给定的项目,如果它被选定为人们所期望有一个独特的名字。但是,对于属于一个组的单选按钮,它们需要具有相同的名称属性,并且由于在上述场景中情况并非如此,所以可以选择这两个单选按钮。这也意味着在选择多个单选按钮时,所有选定单选按钮的selectedId属性将填充它们各自的QuestionId。

为了解决这个问题一个总是可以写代码,所以,

@Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name = "selectone"}) 
@Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"}) 

而且,现在一个开始选择只有一个单选按钮的能力,但随后过程中HttpPost方法反序列化过程中SelectedQuestionId属性各集合中的项目将为空。显然,单选按钮的名称现在是“selectone”,所以在SelectedQuestionId的键/值对中,该值不会被填充,换句话说,即使对于所选的单选按钮,SelectedQuestionId也将始终为空。

因此,解决方案是以下几点:

@Html.RadioButtonFor(x => x.SelectedId, Model.UserResponses[i].QuestionId) 

x.SelectedId在于ParentModel,并获取与问题的QuestionId填充的被选中,因为两者的单选按钮共享相同的名字,他们因此属于同一组,因此只能选择其中一个单选按钮。