Post 操作后视图模型中的空值

Null value in viewmodel after Post Action

我在视图模型的控制器 http post 部分中获取空值。所有的值都是空的。我也无法访问视图模型属性和 select 列表值。如何解决空值并从模型访问 selected 城市以使用数据库上下文更新数据库。

我搜索了其他类似的问题,但这些结果没有帮助。

如果有人能解决这个问题,将会有很大的帮助。

型号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>
    }
        
}


控制器:

[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();

              
            }

首先,你的代码没有正确绑定数据,所以编辑操作无法获取data.Besides,你没有将employeeid传递给操作,所以你可以添加一个隐藏的输入在视图中,然后您可以使用 _context.tblemployee.Find.

这是一个有效的演示:

控制器:

[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();
        }

查看:

@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; }
}

结果: