更新在我的 mvc 项目中不起作用

Update is not working in my mvc project

大家好,我正在尝试更新数据库。但更新在我的项目中不起作用。我使用单个操作创建并且 update.And 我在该项目中使用 kendo 网格控件并编辑弹出模板。所以请检查这个并告诉我为什么更新不起作用。 这是我的代码。

public class ImageController : Controller
    {
        ShoppingContext db = new ShoppingContext();
        string imagename;
        string path;
        // GET: /Image/
        public ActionResult Index()
        {
            return View();
        }
        public JsonResult GetData([DataSourceRequest] DataSourceRequest request)
        {
            var list = db.ImageModels.ToList();
            return Json(list.ToDataSourceResult(request));
        }
        public ActionResult Create(IEnumerable<HttpPostedFileBase> files, ImageModel imageModel)
        {
            foreach (var image in files)
            {
                imagename = Path.GetFileName(image.FileName);
                path = "~/Images/" + imagename;
                var projectpath = Path.Combine(Server.MapPath("~/Images"), imagename);
                image.SaveAs(projectpath);
            }
            var id = imageModel.Imageid;
            if (id > 0)
            {
                imageModel.ImageName = imagename;
                imageModel.ImageUrl = path;
                db.SaveChanges();
            }
            else
            {
                imageModel.ImageName = imagename;
                imageModel.ImageUrl = path;
                db.ImageModels.Add(imageModel);
                db.SaveChanges();
            }
            return RedirectToAction("Index");
        }
    }

我的观点

@(Html.Kendo().Grid<TelerikMVCImageProject.Models.ImageModel>()
.Name("grdImage")
.DataSource(datasource => datasource
    .Ajax()
    .Model(model => model.Id(p=>p.Imageid))
    .Create(create => create.Action("Create", "Image"))
    .Update(update => update.Action("Create", "Image"))
    .Destroy(delete => delete.Action("Delete", "Image"))
    .Read(read => read.Action("GetData", "Image"))
    .ServerOperation(false)
        .Model(model =>
        {
            model.Field(p => p.Imageid).Editable(true);
            model.Id(p => p.Imageid);
            // model.Field(p => p.isenabled).DefaultValue(true);
        })
    )
.Columns(columns =>
{
    //columns.Bound(c => c.IMAGESIZE_ID).ClientTemplate("<input type='checkbox' value #=IMAGESIZE_ID#  />").Width(50);
    columns.Bound(c => c.ProductName).Width(140).Title("Product Name");
    columns.Bound(c => c.ProductDesc).Title("Product Desc");
    columns.Bound(c => c.ImageName).Title("Image Name");
    columns.Bound(c => c.ImageUrl).Title("Image Url");
    columns.Command(command =>
        {
            command.Edit(); 
            command.Destroy();
            //command.Custom("Edit").Action("Update", "Imagetest").Text("Edit");
        });
})
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("ImageModel"))
.HtmlAttributes(new { style = "height: 580px;" })
.Scrollable()
.Sortable()
.Pageable(pageable => pageable
    .Refresh(true)
    .PageSizes(true)
    .ButtonCount(5)
)
)

我的编辑器模板

@model TelerikMVCImageProject.Models.ImageModel
<style>

    .k-state-default {
        visibility:hidden;
    }
</style>
<script>
    function onSelect(e) {
        alert("Select");
        $demo = $("#files").val();
        var filename = $demo.substr(12);
        var path = "~/Images/" + filename;
        $("#divimage").append('<img src = "@Url.Content("~/Images/")' + filename + '">');
        alert(path);
    }
    function onRemove(e) {
        alert("Remove");
        $("#divimage").empty();
    }
</script>
<form method="post" action="@Url.Action("Create")">
<table>
    <tr>
        @Html.HiddenFor(m=>m.Imageid)
    </tr>
    <tr>
        <td>@Html.Label("Product Name")</td>
        <td>@Html.TextBoxFor(m=>m.ProductName)</td>
    </tr>
    <tr>
        <td>@Html.Label("Product Desc")</td>
        <td>@Html.TextBoxFor(m=>m.ProductDesc)</td>
    </tr>
    <tr>
        <td>@Html.Label("Image Desc")</td>
        <td>@(Html.Kendo().Upload()
        .Name("files")
        .Multiple(false)
        .Messages(msg => msg.Select("Browser"))
        //.Async(a=>a.AutoUpload(false).Save("Save","Imagetest"))
        .Events(e=>e.Select("onSelect").Remove("onRemove"))
         )</td>
    </tr>
    <tr>
        <td></td>
        <td><div id="divimage" /></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Save" /></td>
    </tr>
</table>
</form>

这是因为您是从用户那里接收模型,而不是"attaching"它到 EF 数据库上下文。您需要在保存更改之前附加它。

尝试以下操作(在 ImageController 中):

if (id > 0)
{
    db.ImageModels.Attach(imageModel);
    imageModel.ImageName = imagename;
    imageModel.ImageUrl = path;
    db.SaveChanges();
}