0

I generate a partial view

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

when but created control's name start with item.please see bellow image enter image description here in class I have 2 properties

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

and values are not mapped if names are generate like this.

why its generate like this?and how I can prevent this?

kuntal
  • 1,591
  • 2
  • 16
  • 36

2 Answers2

0

Use the code below in your model class:

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

You are mapping the property which is still a collection of string. You need another for loop before you can see the display:

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

You need to do the for loop as well on the ICollection CorrectAnswer property. Lastly, it is not a good idea to have a collection of model with a collection of property. Either you declare one model to your cshtml view with different collections like this one:

@model vm_Master_QuestionSet_AnswerOptions

With collection of properties attached to it:

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

And not this one which is harder to maintain especially if it contains collection of properties:

@model IEnumerable<ViewModel.vm_Master_QuestionSet_AnswerOptions>
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57