开发者

How to determine if ASP.NET MVC has found values for Action parameters

开发者 https://www.devze.com 2023-03-21 23:00 出处:网络
Is it possible to determine if ASP.NET MVC 3 has successfully found any values to set the parameters, or do I need to do this \'manually\'? Options I can see are:

Is it possible to determine if ASP.NET MVC 3 has successfully found any values to set the parameters, or do I need to do this 'manually'? Options I can see are:

  1. Check if the Action's parameters are different from the default values?
  2. Check if there are any values (query string, form variables, etc) with the sa开发者_JAVA百科me name as the parameters?


http://forums.asp.net/p/1620341/4155962.aspx/1?Re+DefaultModelBinder+Scenarios+when+no+values+are+send+to+server


You can easily check the RouteData dictionary to see what was passed as route values. For parameters that don't match the route (those passed in querystring for instance) just check the Request.


Sounds like you might be looking for nullable parameters?

So something like this:

public ActionResult Index(int? param1, string? param2)
{
    if(param1 == null || param2 == null)
        return RedirectToAction("...");
    return View();
}

The question mark after the type simply tell .Net to wrap the type in a special nullable type, which allows you to check if any type has been set simply using the check == null.

Hope this helps, Alex.

0

精彩评论

暂无评论...
验证码 换一张
取 消