写MovieApp时的注意点

本文介绍了在ASP.NET MVC项目中创建视图的过程,包括如何确保控制器中有对应的方法来支持视图,以及如何通过重构提高代码复用率。文中还详细解释了ModelState.IsValid的作用,即检查模型验证状态是否有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

添加第一个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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值