Search code examples
asp.net-mvcasp.net-corerazorasp.net-mvc-viewmodel

Null value in viewmodel after Post Action


I am getting null values in the controller http post part from the view model. All the values are null. I am not able to access the view model properties and select list values as well. How to solve the null values and access the selected city from the model for updating the database using db context.

I searched other similar questions but those results didnt help.

It will be of great help if anyone can solve the issue.

Model Class:

namespace MvcCoreAngular.ViewModels
{
    public class DetailedResultEdit
    {
        public int employeeid { get; set; }
        public string name { get; set; }
        public List<SelectListItem> citieslist { get; set; }
        public int cityid { get; set; }
        public string department { get; set; }
        public string gender { get; set; }

    }

}

HTML:

@model IEnumerable<MvcCoreAngular.ViewModels.DetailedResultEdit>
@{
    ViewData["Title"] = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm("Edit", "Home", FormMethod.Post))
{

    @foreach (var item in Model)
    {
        <table>
            <tr>
                @Html.Label("Name")
                @Html.TextBoxFor(model => item.name)
                <br />
            </tr>
            <tr>
                @Html.Label("Department")
                @Html.TextBoxFor(model => item.department)
                <br />
            </tr>
            <tr>
                @Html.Label("Cities")
                @Html.DropDownListFor(model => item.cityid, item.citieslist, "", null)
                <br />
            </tr>
            <tr>
             <input type="submit" value="Submit" id="btnSubmit" /> 
            </tr>
        </table>
    }
        
}


Controller:

[HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(DetailedResultEdit mo)
        {

            //IEnumerable<tblcities> citieslist = from c in _context.tblcities.ToList<tblcities>().GroupBy(x=> x.cityname).Select(x=> x.First());
            if (ModelState.IsValid) {
                
                    var empdata = _context.tblemployee.Find(mo.employeeid);
                    empdata.cityid = mo.cityid;
                    empdata.department = mo.department;
                    empdata.name = mo.name;
                    _context.SaveChanges();

              
            }

Solution

  • Firstly,your code don't bind the data correctly,so the edit action cannot get the data.Besides,you didn't pass employeeid to action,so you can add a hidden input in the view,and then you can use _context.tblemployee.Find.

    Here is a demo worked:

    Controller:

    [HttpGet]
            public IActionResult Edit() {
                List<tblemployee> tblemployees = _context.tblemployee.ToList();
                List<DetailedResultEdit> detailedResultEdits = new List<DetailedResultEdit>();
                List<SelectListItem> list = new List<SelectListItem> { new SelectListItem { Text = "NY", Value = "1" }, new SelectListItem { Text = "Boston", Value = "2" }, new SelectListItem { Text = "Dover", Value = "3" } };
                foreach (tblemployee t in tblemployees) {
                    DetailedResultEdit temp = new DetailedResultEdit();
                    temp.cityid = t.cityid;
                    temp.name = t.name;
                    temp.employeeid = t.employeeid;
                    temp.department = t.department;
                    temp.gender = t.gender;
                    temp.citieslist = list;
                    detailedResultEdits.Add(temp);
                }
                return View(detailedResultEdits);
            }
            [HttpPost]
            [ValidateAntiForgeryToken]
            public IActionResult Edit(DetailedResultEdit mo)
            {
    
                if (ModelState.IsValid)
                {
                    var empdata = _context.tblemployee.Find(mo.employeeid);
                    empdata.cityid = mo.cityid;
                    empdata.department = mo.department;
                    empdata.name = mo.name;
                    _context.Attach(empdata).State = EntityState.Modified;
                    _context.SaveChanges();
    
                    Edit();
    
    
                }
                return View();
            }
    

    View:

    @model IEnumerable<DetailedResultEdit>
    @{
        ViewData["Title"] = "Edit";
    }
    
    <h2>Edit</h2>
    
    
    @foreach (var item in Model)
    {
        @using (Html.BeginForm("Edit", "TestDB", FormMethod.Post))
        {
    
            <table>
                <tr>
                    <input hidden name="employeeid" value="@item.employeeid" class="form-control" />
                    @Html.Label("Name")
                    <input name="name" value="@item.name" class="form-control" />
                    <br />
                </tr>
                <tr>
                    @Html.Label("Department")
                    <input name="department" value="@item.department" class="form-control" />
                    <br />
                </tr>
                <tr>
                    @Html.Label("Cities")
                    <select name="cityid"
                            asp-for="@item.cityid"
                            asp-items="@item.citieslist">
                    </select>
                    <br />
                </tr>
                <tr>
                    <input type="submit" value="Submit" id="btnSubmit" />
                </tr>
            </table>
        }
    
    }
    

    tblemployee:

    public class tblemployee
    {
        [Key]
        public int employeeid { get; set; }
        public string name { get; set; }
        public int cityid { get; set; }
        public string department { get; set; }
        public string gender { get; set; }
    }
    

    result: enter image description here