更新数据库MVC 5 EF中的多个记录 [英] Update mutiple records in database MVC 5 EF

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

问题描述

晚上好, 现在,我将描述我的情况: 这是我的模特:

Good evening, Now I'll describe my situation: Here is my Model:

[Table("Items")]
public class Item
{
    [Key]
    public string Id { get; set; }
    public DateTime Generated { get; set; }
    public string Content { get; set; }
    public string UrlSeo { get; set; }
    public bool IsActive { get; set; }
    public int Quantity { get; set; }
    public decimal Price { get; set; }
}

我正在对数据库记录执行CRUD操作,但是我需要再执行一个操作,例如,我需要在一个操作中更新选定记录中的 UrlSeo 属性. 所以这是我的看法:

I;m implementing CRUD operations to DB records, but I need implement one more Action, I need to Update for example UrlSeo property in selected records in one action. So here is my view:

@model IEnumerable<CheckBoxes.Models.Item>
@{
ViewBag.Title = "Items";
}
<dv class="page-header">
<h3>Items</h3>
</dv>
<p>
@Html.ActionLink("Create New", "Create")
</p>
@using (Html.BeginForm("ChangeUrl", "Items", FormMethod.Post))
{
<div class="row">
    <div class="form-group-sm">
        <div class="col-md-4">
            @Html.TextBox("urlSeo", null, new { @class = "form-control" })
        </div>
        <div class="col-md-offset-2 col-md-4">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
    <p>

    </p>
</div>
<table class="table">
    <tr>
        <th>
            <input type="checkbox" id="checkAll" />
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Generated)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.UrlSeo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.IsActive)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Quantity)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Price)
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <th>
                <input type="checkbox" class="checkBox" value="@item.Id" />
            </th>
            <td>
                @Html.DisplayFor(modelItem => item.Generated)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.UrlSeo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsActive)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Quantity)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Details", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>
}
@section Scripts{
@Scripts.Render("~/bundles/toastr")
<!--მასიური  წაშლა-->
<script>
    $(document).ready(function () {

        $("#checkAll").click(function () {
            $(".checkBox").prop('checked',
                $(this).prop('checked'));
        });
    });
</script>
}

最后是我的控制器:

[HttpPost]
    public async Task<ActionResult> ChangeUrl(string urlSeo, string[] id)
    {
        foreach (var itemId in id)
        {
            Item item = await db.Items.FindAsync(itemId);
            item.UrlSeo = urlSeo;
            db.Entry(item).State = EntityState.Modified;
            db.Entry(item).Property(x => x.Generated).IsModified = false;
            db.Entry(item).Property(x => x.Content).IsModified = false;
            db.Entry(item).Property(x => x.IsActive).IsModified = false;
            db.Entry(item).Property(x => x.Price).IsModified = false;
            db.Entry(item).Property(x => x.Quantity).IsModified = false;
        }
        await db.SaveChangesAsync();
        return Json(new { success = true });
    }

现在的问题 如何将所有选中的项目传递给控制器​​并从字段输入. 我回到家,有人会帮助我.

Now Question How to pass all checked items to controller and input from field. I home someone will help me.

推荐答案

此处认为有问题的地方有必要为复选框命名,女巫等于控制器的一部分.

Where was problem in view here is necessary give name for checkbox, witch equals part of controller.

@foreach (var item in Model)
{
    <tr>
        <th>
            <input type="checkbox" class="checkBox" value="@item.Id" name="id" />
        </th>

一切正常,是一件事,更好用:

Everything worked fine, yes one more thing, better to use:

return RedirectToAction("Index");

这是可行的解决方案.

这篇关于更新数据库MVC 5 EF中的多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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