另一个大问题,在这里。
我有模型:
public class Lead
{
[System.ComponentModel.DataAnnotations.Column("le_codlead")]
public int LeadID {get; set; }
[System.ComponentModel.DataAnnotations.Column("le_descr1")]
[Required(ErrorMessage="Inserire Nome del Lead")]
[Display(Name="Nominativo Lead")]
public string Name1 {get; set;}
[System.ComponentModel.DataAnnotations.Column("le_descr2")]
[Display(Name = "Nominativo secondario")]
public string Name2 { get; set; }
...
...
...
}
控制器(用于创建方法):
[HttpPost]
public ActionResult Create(Lead lead)
{
if (ModelState.IsValid)
{
db.Leads.Add(lead);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(lead);
}
这是视图:
@model CRMArcadia.Models.Lead
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true, "ATTENZIONE! Compilare tutti i campi obbligatori")
<fieldset>
<legend>Lead</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name1)
@Html.ValidationMessageFor(model => model.Name1)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Name2)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name2)
@Html.ValidationMessageFor(model => model.Name2)
</div>
...
...
...
<p>
<input type="submit" value="Create" />
</p>
</fieldset> }
但是,当我尝试插入新的潜在客户时,它会引发异常,如帖子标题:实体框架:“存储更新、插入或删除语句影响了意外数量的行 (0)。” 我在网上读到它是由 ID 键(在本例中为 LeadID)未更新引起的:他们建议在视图中为 ID 键插入一个 hiddenfor 属性,所以我这样做了:
@Html.HiddenFor(model => model.LeadID)
但是,在“创建”按钮的压力下,没有任何反应。
请问有人对此问题有解决方案(或解决方法;))吗?
谢谢。