I have this View Model:
public class MemberRegisterVM
{
public Users User { get; set; }
public Location Location { get; set; }
}
Which Consists from User
and Location
Models
User:
public partial class Users : Persons
{
public Guid? UserRoleId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public DateTime? RegisterDate { get; set; }
public DateTime ExpireDate { get; set; }
public bool IsLock { get; set; }
public string DigitalSignture { get; set; }
public Guid? BranchID { get; set; }
public int PersonType { get; set; }
public Guid Token { get; set; }
}
public partial class Users
{
public string NewPassword { get; set; }
public string ConfirmPassword { get; set; }
public string ConfirmDigitalSignture { get; set; }
}
Location :
public class Location
{
public int ID { get; set; }
public int ParentId { get; set; }
public int TypeId { get; set; }
public int Code { get; set; }
public string Name { get; set; }
}
For Single Model [for example: User Model]
i know if I use a Standard html input like below it'll send my data to controller as the referenced Model based on Its Name attribute
:
View:
@model User
<input name="UserName" Id="UserName" type="text" />
Controller:
public ActionResult Submit(User user)
{
//user.UserName is filled here
}
Now my question is how to name my Standard HTML elements to send the data to controller as my view model (which consists several models)? using html.helper i will reference it as below for example:
@Html.TextBoxFor(x => x.User.UserName)
Will it be like below?
<input type="Text" name="User.UserName" id="User.UserName">
Or something else? because I've tried above but it still sends value as null on form submit.
Update:
I've excluded other fields on the form, they are based on html.helpers and pass the value correctly, however there are 2 fields that are not working right one would be Gender
and second would be BirthDate
, since User Model inherits from Person model i'll add it here as well:
Person Model:
public class Persons
{
public bool Gender { get; set; }
public Datetime BirthDate { get; set; }
}
Here is my View
@model Models.MemberRegisterVM
@{
Layout = "~/Views/Shared/_SiteLayout.cshtml";
}
@using Entities.Texts;
@using (@Html.BeginForm("Register", "MembershipAuth", FormMethod.Post))
{
<div class="form-horizontal">
<div class="col-sm-6">
<div class="col-sm-12">
<div class="control-label col-sm-2">
@Labels.Gender
</div>
<div class="col-sm-5">
<input name="User.Gender" type="radio" id="men_gender" value="0" />
<label for="men_gender">@Labels.Male</label>
</div>
<div class="col-sm-5">
<input name="User.Gender" type="radio" id="female_gender" value="1" />
<label for="female_gender">@Labels.Female</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="control-label col-sm-2">
@Labels.BirthDay
</div>
<div class="col-sm-10">
<input type="text" name="User.BirthDate" id="User_BirthDate" />
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<div class="g-recaptcha" data-sitekey="6LfLWCkUAAAAAPSp-Fr8bp5egiX8Pw7oKxzZbkmk" style="text-align: center"></div>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Sign Up" />
</div>
</div>
</div>
}
And This is my ActionResult
on controller:
[HttpPost]
[AllowAnonymous]
public ActionResult Register(Models.MemberRegisterVM vm, FormCollection form)
{
//vm.User.Gender is not filled here as well as vm.User.BirthDate
}