【问题标题】:Null value in viewmodel after Post Action后操作后视图模型中的空值
【发布时间】:2020-06-21 17:03:09
【问题描述】:

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

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

如果有人能解决这个问题,那将是非常有帮助的。

模型类:

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

              
            }

【问题讨论】:

    标签: asp.net-mvc asp.net-core razor asp.net-mvc-viewmodel


    【解决方案1】:

    首先,您的代码没有正确绑定数据,因此编辑操作无法获取数据。此外,您没有将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>
        }
    
    }
    

    同事:

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

    结果:

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 2013-01-25
      • 2016-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多