Search code examples
c#asp.net-mvcasp.net-mvc-3editorfor

EditorFor not sending data to controller


I have the following code:

@Html.EditorFor(model => model.NoteRateGreaterThan, null, "advSearchInput", null)

The problem is that when a post is done the value i entered for "NoteRateGreaterThan" is not being populated in the model that is sent to the controller. However when I change it back to:

@Html.EditorFor(model => model.NoteRateGreaterThan) 

it works.

I need to be able to use the first code because i need to be able to change the width of the 'editorfor' input and from my research the only way to do this is through css ('advSearchInput' is the class) Any ideas?


Solution

  • I'm not sure what you think your first example does, but it's not that. EditorFor does not have the ability to specify Html attributes, such as class. You must use a more basic method, like Html.TextBoxFor.

    The third parameter to EditorFor is htmlFieldName, which is used to disambiguate when you have duplicate element ID's.

    That's why your field isn't being populated, because you are changing the name.

    EDIT:

    As of MVC 5.1, EditorFor now allows you to pass html attributes via the follow syntax:

    @Html.EditorFor(model => model.Foo, new { htmlAttributes = new { @class = "whatever" }})