2017-08-26 7 views
0

を生成し、私は部分図モデルforeach要素名が間違って

@model IEnumerable<ViewModel.vm_Master_QuestionSet_AnswerOptions> 

    @foreach (var item in Model) 
    { 
     <div class="form-group"> 
      @Html.LabelFor(m => item.AnswerShownOrder, item.AnswerShownOrder.ToString(), new { @class = "control-label col-md-3 col-sm-3 col-xs-12" }) 
      <div class="col-md-4 col-sm-4 col-xs-4"> 
       @Html.TextBoxFor(m => item.AnswerOptionText, new { @class = "form-control", @id = item.PK_MasterQuestion_AnswerOptionID }) 


      </div> 
      <div class="col-md-2 col-sm-2 col-xs-2"> 

       @Html.CheckBoxFor(m => item.CorrectAnswer, new { @class = "form-control", @id = "chk" + item.PK_MasterQuestion_AnswerOptionID }) 

      </div> 
     </div> 
    } 

生成しますが、コントロールの名前がitem.pleaseで始まる作成したクラスで enter image description here 怒鳴る画像を参照してください私は2つのプロパティ

public ICollection<string> AnswerOptionText { get; set; } 
     public ICollection<bool> CorrectAnswer { get; set; } 

を持っていますこのように名前が生成される場合、値はマップされません。

なぜこのように生成するのですか?これを防ぐにはどうすればよいですか?

答えて

0

は、あなたのモデルクラスに以下のコードを使用します。

[System.ComponentModel.DataAnnotations.Display(Name = "Whatever name you want to assign to it")] 
public ICollection<string> AnswerOptionText { get; set; } 
0

あなたはまだ文字列のコレクションであるプロパティをマッピングしています。あなたがICollection CorrectAnswer性質上だけでなくループのために実行する必要があり

@model IEnumerable<ViewModel.vm_Master_QuestionSet_AnswerOptions> 

@foreach (var item in Model) 
{ 
    <div class="form-group"> 
     @Html.LabelFor(m => item.AnswerShownOrder, item.AnswerShownOrder.ToString(), new { @class = "control-label col-md-3 col-sm-3 col-xs-12" }) 

    @foreach (var itemAnswer in item.AnswerOptionText) 
    { 
     <div class="col-md-4 col-sm-4 col-xs-4"> 
      @Html.TextBoxFor(c => itemAnswer, new { @class = "form-control", @id = item.PK_MasterQuestion_AnswerOptionID }) 
     </div> 
    } 
} 

:あなたは、ディスプレイを見ることができる前に、forループを別のものを必要とします。最後に、プロパティのコレクションを持つモデルのコレクションを持つことは良い考えではありません。それに接続されているプロパティのコレクションで

@model vm_Master_QuestionSet_AnswerOptions 

public ICollection<string> AnswerOptionText { get; set; } 
public ICollection<bool> CorrectAnswer { get; set; } 

そしてないそれならば、特に保守が難しくなり、この1あなたはこのような異なるコレクションを使用してCSHTMLビューへの一つのモデルを宣言どちらかプロパティのコレクションを含んでいます:

@model IEnumerable<ViewModel.vm_Master_QuestionSet_AnswerOptions> 
関連する問題