Question

My one view page I passed in a model through the controller, so I can write:
@Html.DisplayFor(m => m.FirstName) which displays the First Name of the model. When I try to submit the form on the page using

@using (Html.BeginForm("CreateUser", "Controller", FormMethod.Post, new { UserViewModel = Model }))

and I take a look at my model in

[HttpPost]
public virtual ActionResult CreateUser(UserViewModel model)
{
    //model.FirstName is blank

Is there anyway I can make the model.FirstName not blank? by some how passing the model that I originally passed? I could set a bunch of hidden remarks, though if there is a better way that would be very helpful

EDIT: the DisplayFor is just an example to show the model is accessible. I actually have about 15 fields, and I am going through multiple forms trying to populate the model. Is Hidden the only way? and could I just hide the entire model?

Was it helpful?

Solution

@Html.DisplayFor() creates a simple literal with the value by default (if you're not using a display template), on submit only form elements being submitted to the server.

You can use hidden input.

@Html.HiddenFor(m => m.FirstName)

Which will be something like:

<input type="hidden" value="{the first name}" name="FirstName" id="FirstName" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top