Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

@{
    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.

like image 273
rikky Avatar asked Aug 08 '13 12:08

rikky


People also ask

What does HTML BeginForm do?

BeginForm(HtmlHelper, String, String, FormMethod, Object)Writes an opening <form> tag to the response and sets the action tag to the specified controller and action. The form uses the specified HTTP method and includes the HTML attributes.

What is @using HTML BeginForm ()) in MVC?

Html. BeginForm is the Html Helper Extension Method that is used for creating and rendering the form in HTML. This method makes your job easier in creating form. Here, is the method to create a form using Html.

Can we use multiple BeginForm in MVC?

Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.

Which of the following writes an opening tag to the response and sets the action tag to the specified controller action and route values?

BeginForm(RouteValueDictionary) Writes an opening <form> tag to the response and includes the route values from the route value dictionary in the action attribute. The form uses the POST method, and the request is processed by the action method for the view.


2 Answers

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"}
                        });
like image 159
Imad Alazani Avatar answered Nov 15 '22 07:11

Imad Alazani


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.

like image 20
Tommy Avatar answered Nov 15 '22 06:11

Tommy