what is the use of RouteValueDictonary routeValues

2020-02-14 03:22发布

@{
    ViewBag.Title = "About Us";
}

@using (Html.BeginForm(new RouteValueDictionary { {"Action","Index"}}))
{
<input type="submit" value="submit"/>
}

When I render

<form action="/Home/Index" method="post"><input type="submit" value="submit"/>

Is this the actual use of RouteValueDictonary. If so I can do this do by HTML.BeginForm("About","Home")

Can some one explain the actual use of RouteValueDictonary. Any help will be appreciated.

2条回答
一纸荒年 Trace。
2楼-- · 2020-02-14 04:09

This is also useful when implementing the ActionFilterAttribute for Redirection purpose. The basic usage of this class is to define the Action name, Controller Name and Area Name

public class MyActionFilter : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filtercontext)
    {
        filtercontext.Result = new RedirectToRouteResult
            (
                new RouteValueDictionary
                    (
                        new
                        {
                            controller = "ControllerName",
                            action = "ActionName",
                            area = "AreaName"
                        }
                    )
            );
        base.OnResultExecuting(filtercontext);
    }
}

You can also send the Parameter list like below..

filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary
                        {
                            {"action", "ActionName"},
                            {"controller", "ControllerName"},
                            {"area", "Area Name"},
                            {"Parameter Name","Parameter Value"}
                        });
查看更多
来,给爷笑一个
3楼-- · 2020-02-14 04:10

The route value dictionary simply allows flexibility in defining the location that the form will POST to. Not everyone stops at controller/action. For example, let's say that I wanted my form to post to /Area/Controller/FormProcessor/Action/Parameter1/Parameter2. I cannot do this by using the simple Html.BeginForm("Action", "Controller") method.

查看更多
登录 后发表回答