【问题标题】:ModelState.IsValid works on a "new" form, but not on an "edit" formModelState.IsValid 适用于“新”表单,但不适用于“编辑”表单
【发布时间】:2010-11-29 19:37:59
【问题描述】:

我有一个使用实体框架的 ASP.NET MVC 2.0 应用程序。我所有的视图都使用视图模型,其中大多数都很复杂。意思是……要编辑的对象是视图模型的属性,而不是视图模型本身。

我正在使用带有数据注释的部分类,并在控制器的 POST 操作中检查 ModelState.IsValid。

对于一个包含 3 个字段的简单对象,我有一个“新”表单和一个“编辑”表单!

ModelState.IsValid 检查适用于新表单,如果我尝试提交空白表单,则会显示正确的“必填字段”错误。

但是,如果我加载一个 EDIT 表单,并从一些必需的文本框中清除值,然后提交表单,我不会收到验证错误,我只会收到一个异常:

执行处理程序“System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerWrapper”的子请求时出错。

所以我的问题是,ModelState.IsValid 是否不适用于 EDIT 表单,因为它可能正在查看加载的视图模型对象中的值,而不是 FormCollection?



    // this one does not validate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int accountStatusKey, AccountStatusEditViewModel model, FormCollection values)
        {
            if (ModelState.IsValid)
            {
                db.UpdateAccountStatus(accountStatusKey, values);
                return RedirectToAction("States");
            }
            else
            {
                return View("Edit", model);
            }
        }


    // this one does validate

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult New(AccountStatusNewViewModel model, FormCollection values)
        {
            if (ModelState.IsValid)
            {
                db.AddAccountStatus(values);

                return View("States", new AccountStatusStatesViewModel());
            }
            else
            {
                return View("New", model);
            }
        }

    // how I arrive AT the edit form

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(int accountStatusKey)
        {
            return View("Edit", new AccountStatusEditViewModel(accountStatusKey));
        }

    // and finally, the view model code

    public class AccountStatusEditViewModel : ViewModelBase
    {

        public AccountStatus AccountStatus { get; private set; }

        public IEnumerable States { get; private set; }

        public List StatusTypes { get; private set; }

        public AccountStatusEditViewModel(int accountStatusKey)
        {
            AccountStatus = db.GetAccountStatusByKey(accountStatusKey);
            States = db.GetAllStates();

            StatusTypes = new List();
            StatusTypes.Add("Primary Status");
            StatusTypes.Add("Secondary Status");
            StatusTypes.Add("External Status");
        }

        public AccountStatusEditViewModel()
        {
        }

    }

    // this action method does not work at all either - no db updating, no validation
    // the page simply redirects to "States" view, which should only happen if the db
    // was being updated, right?  But nothing is changing in the DB, and the validation
    // never happens.

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(AccountStatusEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (TryUpdateModel(model, "AccountStatus"))
                {
                    return RedirectToAction("States");
                }
                else
                {
                    return View("Edit", model);
                }
            }
            else
            {
                return View("Edit", model);
            }

        }


【问题讨论】:

  • 你能发布这两种操作方法吗?
  • 在您的上一个代码示例中,public ActionResult Edit(AccountStatusEditViewModel model) 出了什么问题?该模型是有效的,并且在重定向到状态视图时会正确更新。当您希望模型包含无效值时,它是否也会这样做?
  • 无论模型是否有效,我都会被重定向到状态视图,并且数据库中没有任何更新。所以 Model.IsValid 似乎总是正确的,即使表单字段为空(无效)。

标签: entity-framework asp.net-mvc-2 entity-framework-4


【解决方案1】:

自从 2.0 版本的 MVC 以来,我不再使用 formcollection。

当我有帖子时,我只在操作的参数中使用视图模型,如下所示:

[HttpPost]
public ActionResult Activatecard(ActivateCardViewModel model)
{

当“它”无法创建我的模型时(为日期时间字段输入 blabla,或者当验证未得到满足时(我使用来自 System.ComponentModel.DataAnnotations 命名空间的验证属性)我得到 ModelState.IsValid 等于 false .

我创建了一个空白的 asp.net mvc2 应用程序,这是标准模板用于登录操作的模型。

【讨论】:

  • 除了这个页面的视图模型很复杂,我试图更新的实际对象是视图模型的属性,而不是视图模型本身。我现在附上视频模型代码。
  • 当您只添加视图模型时,您是否会收到异常,或者它只是不验证您的模型?
【解决方案2】:

请消除 FromCollection 参数。您可以为视图模型使用默认的 ModelBinders。 Asp.net MVC 尝试将表单中的值映射到模型中。

能否请您发布操作方法,它也会引导您进入编辑表单?

【讨论】:

  • 使用 Michel 的方法,见上面的帖子。这应该适合你。
  • 我改成这个样式了,还是什么都不做。数据库没有更新,根本没有进行验证。我会附上上面的代码。
  • 视图看起来如何?在您看来,您使用什么模型?您可以发布在视图中定义模型的部分吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2013-11-03
  • 1970-01-01
  • 1970-01-01
  • 2017-08-30
相关资源
最近更新 更多