I am trying to post a part of my model for a view to action in another controller. I've tried to do it this way:
<% using (Html.BeginForm("Register", "User", new {createDto = Model.UserCreateDto}, FormMethod.Post))
{%>
Form code here...
<%}%>
UserCreateDto is a part of my model and it's being filled in this form. I'm trying to pass it to "Register" action in UsersController:
[HttpPost]
public ActionResult Register(UserCreateDto createDto)
The problem is that route value in Register action is null (createDto) after form submit. Maybe it is happening because route values are being attached to url before form submit event (and before model fields are filled).
Is there any way to do what I want?
UPDATE Here is the actual HTML code:
<form action="/User/Register" id="form1" method="post">
<input id="UserCreateDto_Username" name="UserCreateDto.Username" type="hidden" value="" />
<input id="UserCreateDto_Password" name="UserCreateDto.Password" type="hidden" value="" />
<input id="UserCreateDto_Email" name="UserCreateDto.Email" type="hidden" value="" />
<input id="UserCreateDto_Active" name="UserCreateDto.Active" type="hidden" value="" />
<span class="field-validation-valid" id="UserCreateDto_Username_validationMessage"></span><br /><label for="UserCreateDto_Username">Имя пользователя</label><br /><input id="UserCreateDto_Username" name="UserCreateDto.Username" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Password_validationMessage"></span><br /><label for="UserCreateDto_Password">Пароль</label><br /><input id="UserCreateDto_Password" name="UserCreateDto.Password" type="text" value="" /><br />
<span class="field-validation-valid" id="UserCreateDto_Email_validationMessage"></span><br /><label for="UserCreateDto_Email">E-mail</label><br /><input id="UserCreateDto_Email" name="UserCreateDto.Email" type="text" value="" /><br />
<input type="submit" value="Register"/>
</form>
And here is the updated form code:
<% using (Html.BeginForm("Register", "User", FormMethod.Post))
{%>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Username) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Password) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Email) %>
<%: Html.HiddenFor(userCreateDto => Model.UserCreateDto.Active) %>
<%= Html.LabeledValidatedTextBoxFor(username => Model.UserCreateDto.Username, true) %>
<%= Html.LabeledValidatedTextBoxFor(password => Model.UserCreateDto.Password, true)%>
<%= Html.LabeledValidatedTextBoxFor(email => Model.UserCreateDto.Email, true)%>
<input type="submit" value="Register"/>
<%}%>
Html.LabeledValidatedTextBoxFor is just my custon HTML helper.
Here is updated action signature:
[HttpPost]
public ActionResult Register(UserCreateDto userCreateDto)
userСreateDto in this action is still null. By the way, is there any way not to use hidden fields? They are not secure enough to pass through them user registration information.