Specify/Disambiguate editor field form names with EditorFor<T>
صفحه اصلي

Specify/Disambiguate editor field form names with EditorFor<T>

I am having a slight problem correctly using the EditorFor of MVC 2, it us setting the name of the form field differently depending on context, which causes some headache when posting to a different URL. Here is what happens:

I have a model for a login form:

public class LoginForm {     public string Email { get; set; }     public string Password { get; set; } } 

And then I have a compound view model using this together with some other stuff at the start page:

public class StartPage {     public LoginForm { get; set; }     public SignupForm { get; set; } } 

In the view of the start page (/Home/Index) I do:

<%= Html.EditorFor(m => Model.SignupForm) %> 

This causes the form to render with name="LoginForm.Email", however I let the form post to /Account/LogOn, which only has the LoginForm as model, so I render the form with:

<%= Html.EditorForModel() %> 

This renders the form with name="Email". The LoginForm thus also expects the form key to be Email, but when I post to this url from the startpage it has LoginForm.Email.

I tried using the htmlFieldValue, but it doesn't really help me out since in the /Account/LogOn action it still tried to parse it as Email, and for the start page I need to disambiguate it from the email from the signup form. Also it's not very clean to have to specify this in every place where the view is rendered.

Is there any way to make the form correctly parse this in all cases? Or to force the model to render with a set name regardless of where it is rendered from?

79 out of 100 based on 39 user ratings 359 reviews