【问题标题】:ASP MVC Editing Certain Entities in ModelASP MVC 编辑模型中的某些实体
【发布时间】:2015-11-15 11:50:26
【问题描述】:

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

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();
    }

【问题讨论】:

  • 你好,你应该尝试将@Html.HiddenFor( model =&gt; model.Id)添加到你的html页面。
  • FWIW,您还应该使用 InputModel 而不是直接绑定到您的实体框架模型

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


【解决方案1】:

您发布的表单中应该有一个隐藏字段,该字段将保存 Id 的值。

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

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

为什么会这样?

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

【讨论】:

  • 当然!很容易检查是否是因为表单中缺少值,但我什至没有考虑过。谢谢!
  • @Khaine775 不客气,伙计。我很高兴能帮上忙:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多