ASP.NET MVC的UpdateModel没有更新,但不引发错误 [英] ASP.NET MVC Updatemodel not updating but not throwing error

查看:127
本文介绍了ASP.NET MVC的UpdateModel没有更新,但不引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法,为什么这不更新,但不会引发错误?

 公众的ActionResult编辑(INT ID,[绑定(不包括=DEPTID)的FormCollection集合)
    {
        变种部= _repository.ListOne(ID); //抓斗从记录LINQ to SQL的
        尝试
        {
            的UpdateModel(部门);
            _entities.SubmitChanges();            // TODO:添加更新逻辑这里            返回RedirectToAction(「指数」);
        }
        抓住
        {
            返回查看(部门);
        }
    }


解决方案

有时候,可能出现的情况是一个错误,这不是很好处理的MVC组件内的某个地方扔了,它并不如预期被复制到你的模型状态。然后,当您尝试在视图中显示 Html.ValidationSummary ,它不会告诉你的错误,这是非常令人困惑。可以崩溃这种模式的结合过程中,我已经写<一个个例href=\"http://stackoverflow.com/questions/684445/what-can-cause-viewdata-modelstate-isvalid-to-become-false/928607#928607\">here.通常情况下,你找出为什么发生这种情况后,就可以进行更正到code和不用担心了。

我有以下的code,我使用调试过程中进行考察,让我在它悬停在一个断点,看看是怎么回事:

 公共静态的IDictionary&LT;字符串,字符串&GT; GetModelStateErrors(此的ViewDataDictionary的ViewDataDictionary)
{
    字典&LT;字符串,字符串&GT;字典=新词典&LT;字符串,字符串&GT;();
    的foreach(在viewDataDictionary.ModelState.Keys VAR modelStateKey)
    {
        VAR modelStateValue = viewDataDictionary.ModelState [modelStateKey]
        的foreach(在modelStateValue.Errors VAR误差)
        {
            VAR的errorMessage = error.ErrorMessage;
            VAR例外= error.Exception;
            如果(!String.IsNullOrEmpty(的errorMessage))
            {
                dict.Add(modelStateKey,Egads模型错误消息!+的errorMessage);
            }
            如果(例外!= NULL)
            {
                dict.Add(modelStateKey,Egads模型错误异常!+ exception.ToString());
            }
        }
    }
    返回字典;
}

然后,我可以插入此之后我尝试的UpdateModel,并设置断点就可以了:

  VAR X = ViewData.GetModelStateErrors();

把事情处理好您的来电的UpdateModel 之后。悬停在 X 将显示在模型结合过程中的任何未处理的异常,如果这是真的是这里的问题。

祝你好运!

Any ideas why this doesn't update but doesn't throw an error?

public ActionResult Edit(int id, [Bind(Exclude = "deptid")]FormCollection collection)
    {
        var department = _repository.ListOne(id); //Grabs record from linq to sql
        try
        {
            UpdateModel(department);
            _entities.SubmitChanges();

            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View(department);
        }
    }

解决方案

Sometimes what may happen is an error is thrown somewhere inside of the MVC assembly which is not handled nicely, and which does not get copied into your model state as expected. Then, when you try to display in your view the Html.ValidationSummary, it doesn't show you the error, which can be very confusing. One example that can crash this model binding process I've written about here. Usually, after you figure out why this is happening, you can make the corrections to your code and not worry about it anymore.

I have the following code that I use to inspect during debugging, to let me hover over it at a breakpoint and see what is really going on:

public static IDictionary<string, string> GetModelStateErrors(this ViewDataDictionary viewDataDictionary)
{
    Dictionary<string, string> dict = new Dictionary<string, string>();
    foreach (var modelStateKey in viewDataDictionary.ModelState.Keys)
    {
        var modelStateValue = viewDataDictionary.ModelState[modelStateKey];
        foreach (var error in modelStateValue.Errors)
        {
            var errorMessage = error.ErrorMessage;
            var exception = error.Exception;
            if (!String.IsNullOrEmpty(errorMessage))
            {
                dict.Add(modelStateKey, "Egads! A Model Error Message! " + errorMessage);
            }
            if (exception != null)
            {
                dict.Add(modelStateKey, "Egads! A Model Error Exception! " + exception.ToString());
            }
        }
    }
    return dict;
}

Then, I can insert this after I try to UpdateModel, and set a breakpoint on it:

var x = ViewData.GetModelStateErrors();

Put this right after your call to UpdateModel. Hovering over the x will show you any unhandled exceptions in the model-binding process, if that is what is really the problem here.

Good luck!

这篇关于ASP.NET MVC的UpdateModel没有更新,但不引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆