Home > Back-end >  Data is not binding in ViewModel Nested Class for POST method
Data is not binding in ViewModel Nested Class for POST method

Time:08-04

When saving the details from the view, the details is not binding into the nested class (BOUser) in ViewModel.The nested class data is showing null(Refer the debug result image). I am using .net core 6 in Mac for the development. Please help me out what I am missed in this coding.

Controller Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NearMeBuy.Models.Masters.ViewModels;

namespace NearMeBuyWebApp.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class TestOPSController : Controller
    {
        public IActionResult Index()
        {
            RegisterVM registerVM = new RegisterVM()
            {
                ApplicationRoles = null,
                BOUser = new(),
                DepartmentMaster = null,
                Mode = 1
            };
            return View(registerVM);
        }

        [HttpPost]
        public IActionResult Index(RegisterVM vM )
        {
            if (ModelState.IsValid)
            {
                return View("Hi");
            }
            return View();
        }
    }
}

View Model Class

using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.AspNetCore.Mvc.Rendering;
using NearMeBuy.Models.Tokens.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NearMeBuy.Models.Masters.ViewModels
{
    public class RegisterVM
    {
        public int Mode { get; set; }
        //public RegisterModel RegisterModel { get; set; }
        public BOUser BOUser { get; set; }
        [ValidateNever]
        public IEnumerable<SelectListItem> ApplicationRoles { get; set; }
        [ValidateNever]
        public IEnumerable<SelectListItem> DepartmentMaster { get; set; }
    }
}

BOUser Class

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.EntityFrameworkCore;

namespace NearMeBuy.Models.Masters
{
    public class BOUser
    {
        public string UserId { get; set; }
        [Required(ErrorMessage = "First Name can't be empty")]
        public string FirstName { get; set; }

        [Required(ErrorMessage = "Last Name can't be empty")]
        public string LastName { get; set; }
        [Required(ErrorMessage = "Email can't be empty")]
        public string Email { get; set; }
        [Required(ErrorMessage = "Phone can't be empty")]
        public string PhoneNo { get; set; }
        [Required(ErrorMessage = "Username can't be empty")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Account Confirmation can't be empty")]
        public bool AccountConfirmed { get; set; } = false;

        /// <summary>
        /// The user type field to store the type of user 1: for BackOffice User , 2: for Supplier Registration, 3: for End Users/ Customers.
        /// </summary>
        [Required(ErrorMessage = "User Type Field can't be empty")]
        [Comment("The user type field to store the type of user 1: for BackOffice User , 2: for Supplier Registration, 3: for End Users/ Customers., 0: for Administrator")]
        public Int16 UserType { get; set; }
      
        [Required(ErrorMessage = "Password can't be empty")]
        public string Password { get; set; }
       
        [ValidateNever]
        public string? returnUrl { get; set; }
    }
}

View

@model RegisterVM
@{
    ViewData["Title"] = "Register Page";
}
<form id="registrationForm" asp-controller="TestOPS" asp-action="Index" method="post">
    <div >
        <div >
            <h5 >
                @{
                    @if (@Model.Mode == 1)
                    {
                        <b>Create</b>
                    }
                    else if (@Model.Mode == 2)
                    {
                        <b>Edit</b>
                    }
                    else if (@Model.Mode == 3)
                    {
                        <b>Delete</b>
                    }
                }
            </h5>
        </div>
        <div >
            <div >
                <div >
                    <input type="hidden" id="hdnMode" asp-for="Mode" />
                    <input type="hidden" id="hdnUserId" asp-for="BOUser.UserId" />
                </div>
            </div>


            <div >
                <div >
                    <label>First Name</label>
                </div>
                <div >
                    <input  asp-for="@Model.BOUser.FirstName" name="firstname" />
                    <span asp-validation-for="BOUser.FirstName" ></span>
                </div>
                <div >
                    <label>Last Name</label>
                </div>
                <div >
                    <input  asp-for="BOUser.LastName" name="lastname" />
                    <span asp-validation-for="BOUser.LastName" ></span>
                </div>
            </div>
            <div >
                <div >
                    <label>Email</label>
                </div>
                <div >
                    <input  asp-for="BOUser.Email" name="email" />
                    <span asp-validation-for="BOUser.Email" ></span>
                </div>
                <div >
                    <label>Phone No.</label>
                </div>
                <div >
                    <input  asp-for="BOUser.PhoneNo" name="phoneno" />
                    <span asp-validation-for="BOUser.PhoneNo" ></span>
                </div>
            </div>
            <div >
                <div >
                    <label>User Name</label>
                </div>
                <div >
                    <input  asp-for="BOUser.UserName" name="username" />
                    <span asp-validation-for="BOUser.UserName" ></span>
                </div>
            </div>
            <div >
                <div >
                    <label>Password</label>
                </div>
                <div >
                    <input  asp-for="BOUser.Password" name="password" id="txtPassword" />
                    <span asp-validation-for="BOUser.Password" ></span>
                </div>
            </div>
            <div >
                <div >
                    <label>Confirm Password</label>
                </div>
                <div >
                    <input  name="confirmPassword" />
                    <span asp-validation-for="BOUser.Password" ></span>
                </div>
            </div>
            <div >
                <div >
                    <label>Roles</label>
                </div>
                <div >
                    <table>
                        @if (Model.ApplicationRoles != null)
                        {
                            @foreach (var roles in Model.ApplicationRoles)
                            {
                                <tr>
                                    <td>
                                        <input id="@roles.Value" type="checkbox" name="roles" value="@roles.Value" checked="@roles.Selected" asp-for="@roles.Selected" />
                                    </td>
                                    <td>
                                        <label asp-for="@roles.Value">@roles.Text</label>
                                    </td>
                                </tr>
                            }
                        }
                    </table>
                </div>
            </div>
            <div >
                <div >
                    <label>Department</label>
                </div>
                <div >
                    <table>
                        @if (Model.DepartmentMaster != null)
                        {
                            @foreach (var dept in Model.DepartmentMaster)
                            {
                                <tr>
                                    <td>
                                        <input id="@dept.Value" type="checkbox" name="departments" value="@dept.Value" checked="@dept.Selected" asp-for="@dept.Selected" />
                                    </td>
                                    <td>
                                        <label asp-for="@dept.Value">@dept.Text</label>
                                    </td>
                                </tr>
                            }
                        }
                    </table>
                </div>
            </div>
            <div >
                <div >
                    <label>Approved</label>
                </div>
                <div >
                    <input type="checkbox" asp-for="BOUser.AccountConfirmed" />
                </div>
            </div>
        </div>
        <div >
            
            <input type="submit"  value="Save" />
            
        </div>
    </div>
</form>
@section Scripts{

    <script src="~/js/regLogin.js"></script>
}

Debug Result enter image description here

CodePudding user response:

Remove a name attirbute from ALL web controls, for example should be just

 <input  asp-for="BOUser.LastName"  />

  • Related