0

Pretty simple..or so I thought. This is a plugin page in NopCommerce but I don't think that is at play here.

My Models:

public class CheckBoxModel
{
    public string Name { get; set; }
    public bool Checked { get; set; }
}


public partial class ContactUsModel : BaseNopModel
{
   [AllowHtml]
   [DisplayName("Case Type")]
   public List<CheckBoxModel> CaseType { get; set; }
}

My Controller for a quick check to see if form is displaying correctly:

var model = new ContactUsModel
{
   CaseType = new List<CheckBoxModel>()
   {
      new CheckBoxModel() { Name="Civil Tax", Checked=false },
      new CheckBoxModel() { Name="Criminal Tax", Checked=false },
      new CheckBoxModel() { Name="Other Tax", Checked=false }
   }
}

My razor:

<div class="inputs">
   @Html.LabelFor(model => model.CaseType)
   @for(int i = 0; i < Model.CaseType.Count; i++)
   {
      @Html.LabelFor(lbl => lbl.CaseType[i].Name)
      @Html.CheckBoxFor(chk => chk.CaseType[i].Checked)
   }
</div>

HTML:

Case Type:    Name []      Name[]     Name[]

What am I missing to get the actual name assigned in the controller to show??

0

1 Answer 1

1

Try this:

<div class="inputs">
   @Html.LabelFor(model => model.CaseType)
   @for(int i = 0; i < Model.CaseType.Count; i++)
   {
      @Html.DisplayFor(lbl => lbl.CaseType[i].Name)
      @Html.CheckBoxFor(chk => chk.CaseType[i].Checked)
   }
</div>

LabelFor will print the name of the property, which is not what you're looking for. DisplayFor will print the value.

Sign up to request clarification or add additional context in comments.

1 Comment

What you really want is @Html.LabelFor(m => m.CaseType[i].Checked, Model[i].Name) so that you create a label associated with the checkbox (and displays the value of the associated Name property). And you should replace @Html.LabelFor(m => m.CaseType) with @Html.DisplayNameFor(m => m.CaseType) since you do not have a control named CaseType so a <label> makes no sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.