C# ASP MVC编辑模型中的某些实体
如果我有一个可以创建以下模型中定义的实体的网站:C# ASP MVC编辑模型中的某些实体,c#,asp.net-mvc,entity-framework,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; se
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>
}
这是存储库中的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();
}
在发布的表单中应该有一个隐藏字段,该字段将保存Id的值
@Html.HiddenFor(model=> model.Id)
您可以将此内容放置在您发布的表单中的任何位置
为什么会这样
当您发布表单时,模型数据绑定的时间到了,因为您发布的表单中没有
Id
的任何值,Id
接受它的默认值,即0。您好,您应该尝试将@Html.HiddenFor(model=>model.Id)
添加到您的Html页面中,当然,您还应该使用InputModel,而不是直接绑定到实体框架模型!检查是否是因为表单中缺少值很容易,但我甚至没有想到这一点。谢谢@不客气,伙计。我很高兴我帮了忙:)
@Html.HiddenFor(model=> model.Id)