I'm new to ASP.NET MVC C# development, so I have this code PortalUsersViewModel
public class PortalUsersViewModel
{
public UserInformation User { get; set; }
public Notification Notification { get; set; }
public PageDetails PageDetails { get; set; }
public NewUser NewUser { get; set; }
}
public class NewUser
{
public UserInformation Information { get; set; }
}
public class UserInformation
{
public string UserId { get; set; }
public string Password { get; set; }
public int Status { get; set; }
public int Role { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string RoleDescription { get; set; }
public string Gender { get; set; }
// For change password
public string CPassword { get; set; }
public string OldPassword { get; set; }
public int PwType { get; set; }
}
and my HTML
@using (Html.BeginForm("CreateUser", "Users", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div >
<div >
<input type="text" name="FirstName" required value="@Model.User.FirstName">
<label >First Name<span >*</span></label>
</div>
<div >
<input type="text" name="MiddleName" value="@Model.User.MiddleName">
<label >Middle Name</label>
</div>
<div >
<input type="text" name="LastName" required value="@Model.User.LastName">
<label >Last Name<span >*</span></label>
</div>
<div >
<input type="text" name="LastName" required value="@Model.User.LastName">
<label >Last Name<span >*</span></label>
</div>
<div >
<input type="email" name="Email" required value="@Model.User.Email">
<label >Email<span >*</span></label>
</div>
<div >
<select name="Gender" required>
<option value="" disabled>Please select your gender</option>
<option value="M" @(Model.User.Gender == "Male" ? "selected" : "") )>Male</option>
<option value="F" @(Model.User.Gender == "Female" ? "selected" : "")>Female</option>
</select>
<label >Gender<span >*</span></label>
</div>
</div>
<div >
<div >
<button type="submit"><i ></i> Create</button>
</div>
</div>
}
and my controller is expecting post data like this:
public ActionResult CreateUser(NewUser _user) {
return Json(_user.Information);
}
I want to get data inside NewUser object which have the UserInformation object inside. But whenever I submit post, and printing as JSON I don't receive any. Is there something I need to do in my HTML to submit data inside NewUser -> UserInformation?
Seeking for your advise. Thanks so much in advance.
CodePudding user response:
As you are posting the NewUser
object to API, the NewUser
contains the nested object Information
.
From your form, you need to modify the <input>
element name attribute with the prefix: Information.<Property>
.
So that those input elements' values are passed into the properties of the Information
object.
<div >
<input type="text" name="Information.FirstName" required value="@Model.User.FirstName">
<label >First Name<span >*</span></label>
</div>
<div >
<input type="text" name="Information.MiddleName" value="@Model.User.MiddleName">
<label >Middle Name</label>
</div>
<div >
<input type="text" name="Information.LastName" required value="@Model.User.LastName">
<label >Last Name<span >*</span></label>
</div>
<div >
<input type="text" name="Information.LastName" required value="@Model.User.LastName">
<label >Last Name<span >*</span></label>
</div>
<div >
<input type="email" name="Information.Email" required value="@Model.User.Email">
<label >Email<span >*</span></label>
</div>
<div >
<select name="Information.Gender" required>
...
</select>
<label >Gender<span >*</span></label>
</div>