添加第一个View地,一定要先编译一下整个解决方案,否则不能选到Movie类,手工输入后foreach处会出现红线, 提示不能枚举.
完成例子后要再研究一下
if (!ModelState.IsValid)
return View();
的意思。
还有
Html.ValidationMessage
自己建了一个View ,不成功,原因是,没有在Controller中建相应的方法。
一定要先在Controller中建方法。
如我在HomeController.cs 中建了个方法:
public ActionResult About()
{
return View();
}
然后在Views/Home文件夹下建一个View : About.aspx.
经过前面的学习,我自己建了Detail的view,而且把在edit get,edit post,和detail中都要用到的取movie实体的逻辑重构为一个新的方法,供三方使用。
---
ModelState:model-state dictionary
IsValid: Gets a value that indicates whether this instance of the model-state dictionary is valid.
ModelState Class:Encapsulates the state of model binding to a property of an action-method argument, or to the argument itself.
------------------------
// 摘要:
// Displays a validation message if the specified field contains an error in
// the System.Web.Mvc.ModelStateDictionary.
//
// 参数:
// htmlHelper:
// The HTML helper.
//
// modelName:
// The name of the property or model object being validated.
//
// 返回结果:
// An empty string if valid, otherwise a span with an error message.
public static string ValidationMessage(this HtmlHelper htmlHelper, string modelName);
本文介绍了在ASP.NET MVC项目中创建视图的过程,包括如何确保控制器中有对应的方法来支持视图,以及如何通过重构提高代码复用率。文中还详细解释了ModelState.IsValid的作用,即检查模型验证状态是否有效。
632

被折叠的 条评论
为什么被折叠?



