سؤال

I have a general question about some Razor Syntax I keep finding in a project I am working on...

One of the main helpers being used is: @Html.InputFor

They then insert some Lambda for example: @Html.InputFor(_ => _.User)

My question is, how do I work with this helper (I couldn't find any details via a google search) i.e adding attributes like custom css classes?

Is there a better helper I should be using? (I am new to Razor)

هل كانت مفيدة؟

المحلول

I think you mean @Html.TextBoxFor. That extension method can be found in the InputExtensions class.

How to attach an attribute to it? Use the htmlAttributes property:

@Html.TextBoxFor(x => x.User, htmlAttributes : new { @class = "cssclass" } )

نصائح أخرى

It can be an custom Extension Method applied for HtmlHelper object. Just Have look in the project in a static class.

This link will be very helpful for you

I might be wrong, but i don't think InputFor is a standard MVC helper. Usually you would use either Html.EditorFor() and define an editor template or for say a string input a Html.TextBoxFor() where you can pass html attributes

e.g. Html.TextBoxFor(m => m.User, new { @class="form-control" })

the @class being necessary as class is a c# keyword

HtmlHelper class is used to create common html controls on page.There is no InputFor extended method.

Ex: Below will create text box for model.Name property.

@Html.TextBoxFor(model => model.Name)

See MSDN for more details.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top