what is the use of RouteValueDictonary routeValues in Html.BeginForm()

家住魔仙堡 提交于 2019-11-29 14:45:31

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"}
                        });

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!