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

本文关键字:实体 MVC 编辑 模型 ASP | 更新日期: 2023-09-27 18:22:02

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

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之外的所有字段)。

我的问题是,当我尝试对其进行更改时,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();
    }

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

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

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

您可以将其放置在您发布的表单内的任何位置。

为什么会发生这种情况?

当您发布表单时,模型数据绑定的时间到了,因为您发布的表单中没有任何Id的值,所以Id采用它的默认值,即0。