首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >ASP.NET MVC一次更新数据表字段

ASP.NET MVC一次更新数据表字段
EN

Stack Overflow用户
提问于 2019-03-12 04:12:47
回答 1查看 592关注 0票数 1

我有这个视图,我需要根据值列View: EstimateValue中选择的值来更新表Task.视图接受@model,然后迭代任务列表并在IEnumerable中显示它们。

视图

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

控制器具有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为空,并且我无法更新表Task.我使用JSON将视图中的数据作为数组发送,但TaskList的值仍然为空

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-12 06:24:40

将您的IEnumerable更改为List,因为IEnumerable不允许索引。用@for循环代替@foreach循环。所以你的视图应该是这样的:

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

如果这解决了你的问题,接受答案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55109675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档