一次更新在asp.net的MVC多个记录 [英] Update multiple records at once in asp.net mvc

查看:83
本文介绍了一次更新在asp.net的MVC多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用使一个网站 asp.net mvc的4 &安培; EF6 ,我想所有的一次更新多行。但由于某些原因,它不工作和放大器;我得到了如下的错误,

I'm trying to make a website using asp.net mvc 4 & EF6 where I want to update multiple rows all at once. But for some reason, it's not working & I get an error like this,

System.NullReferenceException:对象不设置到对象的实例

System.NullReferenceException: Object reference not set to an instance of an object

下面是我的codeS,

Here are my codes,

控制器

[HttpPost]
    public ActionResult MakeDue(List<BillCheck> BillLists)
    {
        if (Session["username"] != null)
        {
            if (ModelState.IsValid)
            {
                foreach (var BillId in BillLists)
                {
                    var getDue = db.BillChecks.Where(p => p.id == BillId.id).FirstOrDefault();
                    getDue.due = BillId.due;
                }
                db.SaveChanges();
                return RedirectToAction("Success");
            }
            else
            {
                return RedirectToAction("Failed");
            }
        }
        else
        {
            return RedirectToAction("Login");
        }
    }

查看

@using (Html.BeginForm("MakeDue", "Home"))
{
    @Html.ValidationSummary(true)
    @foreach(var item in Model.DueList)
    {
        @Html.HiddenFor(modelItem => item.id)
        <tr>
            <td>@Html.DisplayFor(modelItem => item.flat)</td>
            <td>@Html.DisplayFor(modelItem => item.name)</td>
            <td>@Html.TextBoxFor(modelItem => item.due)</td>
        </tr>
    }
    <input type="submit" class="btn btn-success" value="Update" />
}

这是一个错误在我的code?我如何更新由于一次给所有的投入?

推荐答案

您第一个问题是您使用的foreach 循环正在产生重复的名称属性,不会绑定到一个集合,因此在 BillLists 参数将始终是一个空集(它也产生重复的 ID 属性是无效的HTML)。你需要使用一个循环或自定义的 EditorTemplate 为typeof运算 BillCheck 。使用循环,你的观点必须

Your first problem is that you use of a foreach loop is generating duplicate name attributes which will not bind to a collection and as a result the BillLists parameter will always be an empty collection (its also generating duplicate id attributes which is invalid html). You need to use a for loop or a custom EditorTemplate for typeof BillCheck. Using a for loop, your view need to be

using (Html.BeginForm("MakeDue", "Home"))
{
  @Html.ValidationSummary(true)
  @for(int i = 0; i < Model.DueList.Count; i++)
  {

    <tr>
      <td>
        @Html.HiddenFor(m => m.DueList[i].id)
        @Html.DisplayFor(m => m.DueList[i].flat)</td>
      <td>@Html.DisplayFor(m => m.DueList[i].name)</td>
      <td>@Html.TextBoxFor(m => m.DueList[i].due)</td>
    </tr>
  }
  <input type="submit" class="btn btn-success" value="Update" />
}

还要注意的是 @ Html.HiddenFor()助手需要一个&LT内部; TD&GT; 元素为了有效的HTML。

Note also that the @Html.HiddenFor() helper need to be inside a <td> element in order to be valid html.

接下来的问题是,在视图模型不是typeof运算列表&LT; BillCheck&GT; ,但它确实包含一个名为属性决斗这是typeof运算列表&LT; BillCheck&GT; 让你的POST方法必须是

The next problem is that the model in the view is not typeof List<BillCheck>, but it does contain a property named DueList which is typeof List<BillCheck> so your POST method needs to be

public ActionResult MakeDue(YourModel model)

其中, YourModel 是您用于生成视图(类名,即在 @model ??? 声明)。然后你在回路控制器的方法必须是

where YourModel is the class name you used to generate the view (i.e. in the @model ??? statement). Then you loop in the controller method need to be

foreach (var BillId in model.DueList)
{
  var getDue = db.BillChecks.Where(p => p.id == BillId.id).FirstOrDefault();
  if (getDue != null) // add this
  {
    getDue.due = BillId.due;
  }
}
db.SaveChanges();

请注意还增设的如果(getDue!= NULL)检查

旁注:你正在检查如果(ModelState.IsValid)。建议您在返回的观点是的ModelState 是无效的,这样用户可以纠正错误。

Side note: Your are checking if (ModelState.IsValid). It is recommended you return the view is ModelState is not valid so that the user can correct any errors.

这篇关于一次更新在asp.net的MVC多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆