在控制器为实体 ARTICLE 创建操作期间,我遇到了简单验证的问题。我首先使用的是 EF 4 数据库。实体 ARTICLE 用作实体 ACTION(ACTION.ARTICLE_id) 中的外键。这就是为什么代码生成工具在实体文章中添加导航属性,即使它没有多大意义。每次我更新实体时,类都会在下面形成(文章)。我再次检查了所有外键。我真的不知道如何处理这个错误来制作干净的解决方案,而不仅仅是清除控制器操作中的错误。一切——甚至视图都是脚手架。
行动:
[HttpPost]
[Authorize(Roles = "ARTICLE_ADMIN")]
public ActionResult Edit(ARTICLE article)
{
if (ModelState.IsValid)
{
article.date_modified = DateTime.Now;
string newimage = this.Request.Form["preview_image_filename"];
string oldimage = this.Request.Form["original_image_filename"];
if (newimage.NotNullOrEmpty())
{
article.preview_image = newimage;
}
else
{
article.preview_image = oldimage;
}
db.Entry(article).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
article.date_modified = DateTime.Now;
ViewBag.ARTICLE_CATEGORY_id = new SelectList(db.ARTICLE_CATEGORY, "ARTICLE_CATEGORY_id", "description", article.ARTICLE_CATEGORY_id);
ViewBag.ARTICLE_STATUS_id = new SelectList(db.ARTICLE_STATUS, "ARTICLE_STATUS_id", "description", article.ARTICLE_STATUS_id);
ViewBag.WEB_USER_id = new SelectList(db.WEB_USER, "WEB_USER_id", "login", article.WEB_USER_id);
return View(article);
}
我正在使用通过代码生成工具生成的这个实体模型,并在元数据类中添加了注释,它再简单不过了
public partial class ARTICLE
{
public ARTICLE()
{
this.PROGRAM_WEEK_DAY_ITEM = new HashSet<PROGRAM_WEEK_DAY_ITEM>();
this.STORAGE = new HashSet<STORAGE>();
this.SHOW = new HashSet<SHOW>();
this.ACTION = new HashSet<ACTION>();
}
public int ARTICLE_id { get; set; }
public System.DateTime date_created { get; set; }
public Nullable<System.DateTime> date_modified { get; set; }
public string title { get; set; }
public string html { get; set; }
public int WEB_USER_id { get; set; }
public int ARTICLE_STATUS_id { get; set; }
public int ARTICLE_CATEGORY_id { get; set; }
public Nullable<System.DateTime> date_published { get; set; }
public string preview_image { get; set; }
//code generation tool added those navigation props
public virtual ARTICLE_CATEGORY ARTICLE_CATEGORY { get; set; }
public virtual ARTICLE_STATUS ARTICLE_STATUS { get; set; }
public virtual WEB_USER WEB_USER { get; set; }
public virtual ICollection<PROGRAM_WEEK_DAY_ITEM> PROGRAM_WEEK_DAY_ITEM { get; set; }
public virtual ICollection<STORAGE> STORAGE { get; set; }
public virtual ICollection<SHOW> SHOW { get; set; }
//this one causes trouble I think, but no clue why
public virtual ICollection<ACTION> ACTION { get; set; }
}
元数据类 - 只显示名称和格式:
public class ARTICLE_Metadata
{
[Key]
public int ARTICLE_id { get; set; }
[Display(Name="Vytvořeno")]
public System.DateTime date_created { get; set; }
[Display(Name = "Změněno")]
public Nullable<System.DateTime> date_modified { get; set; }
[Display(Name = "Publikováno")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public Nullable<System.DateTime> date_published { get; set; }
[Display(Name = "Titulek článku")]
public string title { get; set; }
[Display(Name = "Obsah článku")]
[UIHint("tinymce_full"), AllowHtml]
public string html { get; set; }
[Display(Name = "Vytvořil")]
public int WEB_USER_id { get; set; }
[Display(Name = "Status")]
public int ARTICLE_STATUS_id { get; set; }
[Display(Name = "Kategorie")]
public int ARTICLE_CATEGORY_id { get; set; }
[Display(Name = "Náhledový obrázek")]
public string preview_image { get; set; }
}
最后在剃刀视图中形成:
@using (Html.BeginForm("Create", "Articles", FormMethod.Post, new { @class = "base-form" }))
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Nový článek</legend>
@Html.DatePickerFor(model => model.date_published, false)
@Html.HiddenFor(model => model.WEB_USER_id)
<p class="editor-label">
@Html.LabelFor(model => model.ARTICLE_STATUS_id)
@Html.DropDownList("ARTICLE_STATUS_id")
@Html.ValidationMessageFor(model => model.ARTICLE_STATUS_id)
</p>
<p class="editor-label">
@Html.LabelFor(model => model.ARTICLE_CATEGORY_id)
@Html.DropDownList("ARTICLE_CATEGORY_id")
@Html.ValidationMessageFor(model => model.ARTICLE_CATEGORY_id)
</p>
<p class="editor-label">
@Html.LabelFor(model => model.title)
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</p>
<div class="html-editor">
@Html.EditorFor(model => model.html)
</div>
<p>
<input type="submit" value="Vytvořit" class="submit" />
</p>
</fieldset>
}
当模型验证并进入控制器操作时,ModelState.IsValid == false,ModelState 声明属性 ACTION 错误,该属性甚至不存在于表中且不应该存在,它是导航属性。
错误有一个例外:从类型“System.String”到类型“namespace.ACTION”的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。
我试图附加调试器视图图像,但这个网站不允许我这样做。我有其他实体通过控制器管理并以相同的方式查看 - 大约 30 个没有发生这种情况。
如何在不创建具有相同属性但没有导航属性的额外模型的情况下摆脱这个问题?或者只是阻止此导航属性包含在验证中。或者它是一个新的微软废话功能?