ASP.NET MVC验证和视图

时间:2021-06-10 16:38:50

I'm using MVC to validate some html text boxes on a page, for example in my controller there is

我正在使用MVC来验证页面上的一些html文本框,例如在我的控制器中

        if (String.IsNullOrEmpty(name))
        {
            ModelState.AddModelError("name", "You must specify a name.");
        }


        if (ViewData.ModelState.IsValid)
        {
            return RedirectToAction("Index");
        }

return View();

the problem is here, if validation fails, it fails returning View("Add") reason being controllers don't process views on return view(), an option would be to use RedirectToView("viewname"); and that'll work fine EXCEPT it doesn't carry through the validation AddModelError stuff ("it's as if loading the page for the first time").

问题出在这里,如果验证失败,则返回View(“添加”)失败的原因是控制器不在返回视图()上处理视图,一个选项是使用RedirectToView(“viewname”);并且它会正常工作除了它没有通过验证AddModelError的东西(“它就好像第一次加载页面”)。

I can get round this by repeating the code for populating SelectList boxes before the return View();

我可以通过在返回View()之前重复填充SelectList框的代码来解决这个问题。

like this

        ViewData["rooms"] = new SelectList(Villa.intList(10));
        ViewData["sleeps"] = new SelectList(Villa.intList(20));
        ViewData["accomodationType"] = new SelectList(accomodationList, "accomodationId", "accomodationType");
        ViewData["regionName"] = new SelectList(regionList, "regionId", "regionName");
        return View();

that works fine, however, I think there is a better way rather than repeating that block of code, does anyone know any way of returning a redirected view and passing it the model errors?

但是,我认为有一种更好的方法而不是重复那段代码,有没有人知道任何返回重定向视图并传递模型错误的方法?

Thanks in advance, hope it made some kind of sense.

在此先感谢,希望它有一些意义。

2 个解决方案

#1


4  

Take the code you have for initializing the ViewData in the (GET) Add action, and refactor it (extract method) into a standalone, private method. Call that method from your (GET) Add action. Now in the POST action (which is, I presume, what you're showing above; it isn't clear) you can call the same, private method to populate ViewData. Now you no longer have any duplicated code. Remember that ViewData is a property of the Controller type, so you can set it anywhere, not just in the action method itself.

获取用于在(GET)Add操作中初始化ViewData的代码,并将其重构(提取方法)为独立的私有方法。从您的(GET)添加操作中调用该方法。现在在POST操作中(我猜想,你在上面显示的内容;目前尚不清楚)你可以调用相同的私有方法来填充ViewData。现在您不再有任何重复的代码。请记住,ViewData是Controller类型的属性,因此您可以将其设置在任何位置,而不仅仅是在action方法本身。

#2


0  

I must admit, I have some confusion following exactly what you mean, so this is sort of a generic answer that might not be exact!

我必须承认,我对你的意思有些疑惑,所以这是一个通用的答案,可能不准确!

http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx

This is a good read.

这是一个很好的阅读。

I can think of two ways.

我可以想到两种方式。

To change the least amount of code, just put your ViewData in TempData and after the Redirect retrieve it.

要更改最少量的代码,只需将ViewData放在TempData中,然后在重定向后检索它。

The probably more accepted answer is to use the method described in the link above. Have your form post back to the same action. That action will have two implementations -- a Post and a Get. In the Post action, do all validation logic. If the validation works, do a redirect action to whatever view you display on success (Post-Redirect-Get pattern). If the validation fails, redisplay the same view of the form with the validation errors displayed.

可能更容易接受的答案是使用上面链接中描述的方法。让您的表单回复相同的操作。该操作将有两个实现 - 一个Post和一个Get。在Post操作中,执行所有验证逻辑。如果验证有效,请对成功显示的任何视图执行重定向操作(Post-Redirect-Get模式)。如果验证失败,则重新显示表单的相同视图,并显示验证错误。

If this isn't what you are asking, lemme know~

如果这不是你问的问题,那就知道〜

#1


4  

Take the code you have for initializing the ViewData in the (GET) Add action, and refactor it (extract method) into a standalone, private method. Call that method from your (GET) Add action. Now in the POST action (which is, I presume, what you're showing above; it isn't clear) you can call the same, private method to populate ViewData. Now you no longer have any duplicated code. Remember that ViewData is a property of the Controller type, so you can set it anywhere, not just in the action method itself.

获取用于在(GET)Add操作中初始化ViewData的代码,并将其重构(提取方法)为独立的私有方法。从您的(GET)添加操作中调用该方法。现在在POST操作中(我猜想,你在上面显示的内容;目前尚不清楚)你可以调用相同的私有方法来填充ViewData。现在您不再有任何重复的代码。请记住,ViewData是Controller类型的属性,因此您可以将其设置在任何位置,而不仅仅是在action方法本身。

#2


0  

I must admit, I have some confusion following exactly what you mean, so this is sort of a generic answer that might not be exact!

我必须承认,我对你的意思有些疑惑,所以这是一个通用的答案,可能不准确!

http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx

This is a good read.

这是一个很好的阅读。

I can think of two ways.

我可以想到两种方式。

To change the least amount of code, just put your ViewData in TempData and after the Redirect retrieve it.

要更改最少量的代码,只需将ViewData放在TempData中,然后在重定向后检索它。

The probably more accepted answer is to use the method described in the link above. Have your form post back to the same action. That action will have two implementations -- a Post and a Get. In the Post action, do all validation logic. If the validation works, do a redirect action to whatever view you display on success (Post-Redirect-Get pattern). If the validation fails, redisplay the same view of the form with the validation errors displayed.

可能更容易接受的答案是使用上面链接中描述的方法。让您的表单回复相同的操作。该操作将有两个实现 - 一个Post和一个Get。在Post操作中,执行所有验证逻辑。如果验证有效,请对成功显示的任何视图执行重定向操作(Post-Redirect-Get模式)。如果验证失败,则重新显示表单的相同视图,并显示验证错误。

If this isn't what you are asking, lemme know~

如果这不是你问的问题,那就知道〜