0

I have login view that takes a LoginPageViewModel:

public class LoginPageViewModel : PageViewModel
{
    public string ReturnUrl { get; set; }

    public bool PreviousLoginFailed { get; set; }

    public LoginFormViewModel EditForm { get; set; }
}

which is rendered in the view. When a user tries to log in I only want to post the LoginFormViewModel (Model.EditForm) to the controller:

public ActionResult Login(LoginFormViewModel loginDetails)
{
     //do stuff
}

Using Html.TextBox I can specify the name of the form field manually 'loginDetails.UserName' and post back to the controller and everything works.

@model Web.Controllers.User.ViewModels.LoginPageViewModel

@using (Html.BeginForm()){
@Html.Hidden("loginDetails.ReturnUrl", Model.ReturnUrl)

    @Html.LabelFor(x => x.EditForm.UserName, "User Name:")
    @Html.TextBox("loginDetails.UserName", Model.EditForm.UserName)
    @Html.ValidationMessageFor(x => x.EditForm.UserName)

.....

But what I want to do is to use the staticaly typed helper, something like:

@Html.TextBoxFor(x => x.EditForm.UserName)

But I'm unable to get this to work.

Are you only able to post back the same model when useing the strongly typed helpers? Is there something I'm missing on this? Intellisense doesn't seem to give any clues such as a form field string.

1 Answer 1

1

You could specify a prefix so that the default model binder can successfully resolve the property:

[HttpPost]
public ActionResult Login(
    [Bind(Prefix = "EditForm")]LoginFormViewModel loginDetails
)
{
    ...
}

Now feel free to use the strongly typed TextBoxFor or even better: the EditorFor helper.

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.