ASP MVC编辑模型中的某些实体

时间:2015-11-15 11:50:27

标签: c# asp.net-mvc entity-framework

如果我有一个网站,我可以在其中创建以下模型中定义的实体:

public partial class Component
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Info { get; set; }
    public string ManufacturerLink { get; set; }
    public string Datasheet { get; set; }
}

现在,我希望能够从我的网站编辑这个实体,我认为我按顺序完成了大部分实体,因为我能够编辑我想要的字段(除Id之外的所有字段)。 / p>

我的问题是,当我尝试对其进行更改时,Id将设置为0,这将始终只使用Id 0更新数据库表中的行。如何从我想要的实体中保留Id改变?

这是我的编辑视图:

@using (Html.BeginForm("Edit", "Edit", FormMethod.Post, new {enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()

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

    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Info, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Info, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Info, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Image, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @{
                var base64 = Convert.ToBase64String(Model.Image);
                var imgSrc = String.Format("data:image/gif;base64,{0}", base64);
            }

            <img src="@imgSrc" height="300px" width="300px" alt="No image to display." />
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.ManufacturerLink, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.ManufacturerLink, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.ManufacturerLink, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Datasheet, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Datasheet, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Datasheet, "", new { @class = "text-danger" })
        </div>
    </div>

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

这是我在控制器中的操作方法,单击“保存更改”输入按钮时会调用该方法:

    [HttpPost]
    public ActionResult Edit(Component component)
    {
        if (ModelState.IsValid)
        {
            componentRepository.UpdateComponent(component);
            return RedirectToAction("Index", component.Id);
        }
        return View("Index", component);
    }

这是来自存储库的UpdateComponent:

    public void UpdateComponent(Component component)
    {
        Component comp = mContext.Components.Find(component.Id);

        if (comp != null)
        {
            comp.Name = component.Name;
            comp.Info = component.Info;
            comp.Datasheet = component.Datasheet;
            comp.ManufacturerLink = component.ManufacturerLink;
        }
        mContext.SaveChanges();
    }

1 个答案:

答案 0 :(得分:4)

您发布的表单中应该有一个隐藏字段,其中包含Id的值。

@Html.HiddenFor(model=> model.Id)

您可以将其放在里面您发布的表单中。

  

为什么会这样?

当您发布表单并且模型数据绑定的时间到来时,由于您为Id发布的表单中没有任何值,Id会将其视为{&#39}。 s默认值,即0。