更新在我的mvc项目中无效

时间:2015-01-12 11:23:46

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller kendo-grid

大家好我要更新数据库。但更新不适用于我的项目。我使用单个动作来创建和更新。我在这个项目中使用了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>

1 个答案:

答案 0 :(得分:1)

这是因为您从用户接收模型而不是将其“附加”到EF db上下文。您需要在保存更改之前附加它。

尝试以下(在ImageController中):

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