1

What's difference between:

@Html.LabelFor(model => model.Soluongton, htmlAttributes: new { @class = "control-label col-md-2" })

and

@Html.LabelFor(model => model.Soluongton, new { htmlAttributes = new { @class = "control-label col-md-2" } })  

and what's htmlAttributes?

2
  • 4
    Look at the actual html it generates and you will see the 2nd one is wrong. Commented Oct 4, 2018 at 6:31
  • 2
    new { htmlAttributes = new { @class = "control-label col-md-2" } } => this is only used in EditorFor. LabelFor uses the first one. Commented Oct 4, 2018 at 6:32

1 Answer 1

1
@Html.LabelFor(model => model.Soluongton, htmlAttributes: new { @class = "control-label col-md-2" })

will render the HTML code:

<label class="control-label col-md-2" for="Soluongton">Soluongton</label>

@Html.LabelFor(model => model.Soluongton, new { htmlAttributes = new { @class = "control-label col-md-2" } })  

will render the HTML code:

<label for="Soluongton" htmlAttributes="{ class = control-label col-md-2 }">Soluongton</label>

As you can see the second @Html.LabelFor will render a wrong HTML output.

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

Comments

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.