服务器端验证,如何传递错误查看MVC? [英] server side validation, how to pass errors to view in MVC?

查看:126
本文介绍了服务器端验证,如何传递错误查看MVC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用.NET MVC和我做我的行动某些服务器端验证处理表单职位。

Using .net MVC and I am doing some server side validation in my Action that handles the form post.

什么是一个很好的技术来传递错误回到看法?

What is a good technique to pass the errors back to the view?

我想创建一个错误集合,然后添加收藏到我的ViewData然后莫名其妙地编织一些JavaScript(使用jQuery)显示错误。

I am thinking of creating an error collection, then adding that collection to my ViewData and then somehow weave some javascript (using jQuery) to display the error.

这将是很好的jQuery它显示了一些AUTOMAGIC方式错误,因为这是一个常见的​​模式。

It would be nice it jQuery had some automagic way of display an error since this is a common pattern.

你是干什么的?

推荐答案

您希望将错误添加到ModelState中为@Mehrdad表示。

You want to add the errors to the ModelState as @Mehrdad indicates.

...
catch (ArgumentOutOfRangeException e)
{
    ModelState.AddModelError( e.ParamName, e.Message );

    result = View( "New" );
}

和包括在ValidationSummary在视图

And include the ValidationSummary in your View

<%= Html.ValidationSummary() %>

这篇关于服务器端验证,如何传递错误查看MVC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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