ASP.NET MVC同时更新数据表的字段

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (134)

我有这个视图,我需要在Value Column View:EstimateValue上更新所选值的表Task 。该视图接受@model IEnumerable,然后迭代任务列表并在Table中显示它们。

@model IEnumerable<Task>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <table class="table table-striped table-hover table-bordered">
            <thead>
                <tr class="info">
                    <th>
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Description)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => model.Value)           
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        @Html.HiddenFor(modelItem => item.ID)
                        <td>
                            @Html.DisplayFor(modelItem => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => item.Description)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => item.Value, new { @class = "form-control" })
                            @Html.ValidationMessageFor(modelItem => item.Value, "", new { @class = "text-danger" })
                        </td>
                    </tr>
                }
            </tbody>
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

Controller具有post方法EstimateValue

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult EstimateValue(IEnumerable<Task> TaskList)
    {
        if (ModelState.IsValid)
        {
            foreach (var task in TaskList)
            {
                db.Entry(task).State = EntityState.Modified;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        //1. Rebound List to view
        var TaskList = db.Tasks.ToList();
        //2, return model to view
        return View(TaskList);
    }

运行应用程序时,TaskList为null,我无法更新表任务。我使用JSON将视图中的数据作为数组发送,但仍然是TaskList的值为null

提问于
用户回答回答于

将IEnumerable更改为List,因为IEnumerable不允许索引。而不是@foreach循环,使用@for循环。所以你的View应该是这样的:

@model List<Task>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <table class="table table-striped table-hover table-bordered">
            <thead>
                <tr class="info">
                    <th>
                        @Html.DisplayNameFor(model => Model[0].Name)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => Model[0].Description)
                    </th>
                    <th>
                        @Html.DisplayNameFor(model => Model[0].Value)           
                    </th>
                </tr>
            </thead>
            <tbody>
                @for(int i=0;i<Model.Count;i++)
                {
                    <tr>
                        @Html.HiddenFor(modelItem => modelItem[i].ID)
                        <td>
                            @Html.DisplayFor(modelItem => modelItem[i].Name)
                        </td>
                        <td>
                            @Html.DisplayFor(modelItem => modelItem[i].Description)
                        </td>
                        <td>
                            @Html.EditorFor(modelItem => modelItem[i].Value, new { @class = "form-control" })
                            @Html.ValidationMessageFor(modelItem => modelItem[i].Value, "", new { @class = "text-danger" })
                        </td>
                    </tr>
                }
            </tbody>
        </table>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

然后更改您的控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult EstimateValue(List<Task> TaskList)
    {
        if (ModelState.IsValid)
        {
            for(int i=0;i<TaskList.Count;i++)
            {
                db.Entry(TaskList[i]).State = EntityState.Modified;
                db.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        //1. Rebound List to view
        var TaskList = db.Tasks.ToList();
        //2, return model to view
        return View(TaskList);
    }

如果这样可以解决您的问题,请接受答案。

用户回答回答于

在您的视图中,尝试在@ Html.HiddenFor(modelItem => item.ID)下添加以下两行

@Html.HiddenFor(modelItem => item.Name)
@Html.HiddenFor(modelItem => item.Description)

扫码关注云+社区

领取腾讯云代金券