在 ASP.net MVC 单元测试中访问 ModelState 错误字典中的错误消息

发布于 2024-10-31 05:59:26 字数 700 浏览 7 评论 0原文

我在操作结果中添加了一个键值对,如下所示:

[HttpPost, Authorize]
        public ActionResult ListFacilities(int countryid)
{
...
        ModelState.AddModelError("Error","No facilities reported in this country!");
...
}

我在单元测试中有一些像这样的繁琐代码:

 public void ShowFailforFacilities()
 {
    //bogus data
    var facilities = controller.ListFacilities(1) as PartialViewResult;


    Assert.AreSame("No facilities reported in this country!",
        facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage);

 }

当然,只要我只有一个错误,它就可以工作。
我不喜欢facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage

有没有更简单的方法可以从该字典中获取值?

I have added a key-value pair in the action result like this:

[HttpPost, Authorize]
        public ActionResult ListFacilities(int countryid)
{
...
        ModelState.AddModelError("Error","No facilities reported in this country!");
...
}

I have some cumbersome codes like these in a unit test to :

 public void ShowFailforFacilities()
 {
    //bogus data
    var facilities = controller.ListFacilities(1) as PartialViewResult;


    Assert.AreSame("No facilities reported in this country!",
        facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage);

 }

Of course, it works whenever I have only one error.
I don't like facilities.ViewData.ModelState["Error"].Errors.FirstOrDefault().ErrorMessage.

Is there an easier way for me to fetch the value from that dictionary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满身野味 2024-11-07 05:59:26

不需要您的 FirstOrDefault,因为您在访问 ErrorMessage 时会收到 NullReferenceException。您只需使用 First() 即可。

不管怎样,我找不到任何内置的解决方案。相反,我所做的是创建一个扩展方法:

public static class ExtMethod
    {
        public static string GetErrorMessageForKey(this ModelStateDictionary dictionary, string key)
        {
            return dictionary[key].Errors.First().ErrorMessage;
        }
    }

其工作原理如下:

ModelState.GetErrorMessageForKey("error");

如果您需要更好的异常处理,或支持多个错误,它很容易扩展...

如果您希望它更短,您可以创建一个扩展方法对于 ViewData...

public static class ExtMethod
    {
        public static string GetModelStateError(this ViewDataDictionary viewData, string key)
        {
            return viewData.ModelState[key].Errors.First().ErrorMessage;
        }
    }

和用法:

ViewData.GetModelStateError("error");

Your FirstOrDefault isn't needed, because you'll get a NullReferenceException when accessing ErrorMessage. You can just use First().

Either way, I couldn't find any built-in solution. What I've done instead is create an extension method:

public static class ExtMethod
    {
        public static string GetErrorMessageForKey(this ModelStateDictionary dictionary, string key)
        {
            return dictionary[key].Errors.First().ErrorMessage;
        }
    }

Which works like this:

ModelState.GetErrorMessageForKey("error");

If you need better exception handling, or support for multiple errors, its easy to extend...

If you want this to be shorter you can create an extension method for the ViewData...

public static class ExtMethod
    {
        public static string GetModelStateError(this ViewDataDictionary viewData, string key)
        {
            return viewData.ModelState[key].Errors.First().ErrorMessage;
        }
    }

and usage:

ViewData.GetModelStateError("error");
給妳壹絲溫柔 2024-11-07 05:59:26

你试过这个吗?

// Note: In this example, "Error" is the name of your model property.
facilities.ViewData.ModelState["Error"].Value
facilities.ViewData.ModelState["Error"].Error

Have you tried this?

// Note: In this example, "Error" is the name of your model property.
facilities.ViewData.ModelState["Error"].Value
facilities.ViewData.ModelState["Error"].Error
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文