0

I'm new to ASP.NET MVC development, so I have 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 class="form-row">
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="FirstName" required value="@Model.User.FirstName">
            <label class="small font-weight-bold custom-form-label">First Name<span class="text-danger">*</span></label>
        </div>
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="MiddleName" value="@Model.User.MiddleName">
            <label class="small font-weight-bold custom-form-label">Middle Name</label>
        </div>
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="LastName" required value="@Model.User.LastName">
            <label class="small font-weight-bold custom-form-label">Last Name<span class="text-danger">*</span></label>
        </div>
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="LastName" required value="@Model.User.LastName">
            <label class="small font-weight-bold custom-form-label">Last Name<span class="text-danger">*</span></label>
        </div>
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <input type="email" class="form-control form-control-sm custom-input" name="Email" required value="@Model.User.Email">
            <label class="small font-weight-bold custom-form-label">Email<span class="text-danger">*</span></label>
        </div>
        <div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
            <select name="Gender" class="custom-select custom-select-sm custom-select-1" 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 class="small font-weight-bold custom-form-label">Gender<span class="text-danger">*</span></label>
        </div>
    </div>
    <div class="form-row">
        <div class="col-12 col-sm-3 text-center mt-2 mb-2 form-group mx-auto">
            <button class="btn btn-sm btn-block custom-btn-dark" type="submit"><i class="fas fa-fw fa-plus-circle"></i> Create</button>
        </div>
    </div>
}

and my controller is expecting to receive the posted data like this:

public ActionResult CreateUser(NewUser _user) {
    return Json(_user.Information);
}

I want to get data inside the NewUser object which have the UserInformation object inside. But whenever I submit as post, and print as JSON I don't receive any. Is there something I need to do in my HTML to submit data inside NewUser -> UserInformation?

Seeking your advice. Thanks so much in advance.

2
  • Can you share the controller especially the CreateUser method? If you debug, does it hit the CreateUser API? Commented Oct 19, 2022 at 5:03
  • Hi, yes. Wait let me edit the post and include the controller. Commented Oct 19, 2022 at 5:06

1 Answer 1

1

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. (Overall the name attribute will be as: Information.<Property>.

So that those input elements' values are passed into the properties of the Information object.

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="Information.FirstName" required value="@Model.User.FirstName">
    <label class="small font-weight-bold custom-form-label">First Name<span class="text-danger">*</span></label>
</div>

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="Information.MiddleName" value="@Model.User.MiddleName">
    <label class="small font-weight-bold custom-form-label">Middle Name</label>
</div>

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="Information.LastName" required value="@Model.User.LastName">
    <label class="small font-weight-bold custom-form-label">Last Name<span class="text-danger">*</span></label>
</div>

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <input type="text" class="form-control form-control-sm text-capitalize custom-input" name="Information.LastName" required value="@Model.User.LastName">
    <label class="small font-weight-bold custom-form-label">Last Name<span class="text-danger">*</span></label>
</div>

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <input type="email" class="form-control form-control-sm custom-input" name="Information.Email" required value="@Model.User.Email">
    <label class="small font-weight-bold custom-form-label">Email<span class="text-danger">*</span></label>
</div>

<div class="col-12 col-sm-6 mt-2 mb-2 form-group custom-form-group">
    <select name="Information.Gender" class="custom-select custom-select-sm custom-select-1" required>
        ...
    </select>
    <label class="small font-weight-bold custom-form-label">Gender<span class="text-danger">*</span></label>
</div>
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.