Using Html.BeginForm with querystring

前端 未结 7 1563
无人共我
无人共我 2021-01-01 09:44

My url looks like this:

customer/login?ReturnUrl=home

In the login view, I have used this pattern of code which works fine.



        
相关标签:
7条回答
  • 2021-01-01 10:13

    Try @using(Html.BeginForm(null, null, FormMethod.Post, new { data_id="something" }))

    It should use the default logic to construct the url, just as if you used BeginForm()

    (never tried that though in such case, but I believe it should work)

    0 讨论(0)
  • 2021-01-01 10:19

    Here's The way that worked for me

    Html.BeginForm("Profile", "Partner", routeValues: new {id=Partner.partner_id},method:FormMethod.Post)
    

    It was almost like there was a problem with overloading the method, but by specifying what things are, it seems to work fine...

    0 讨论(0)
  • 2021-01-01 10:19

    using Reflector to look at the code,

    BeginForm() will pass directly the rawUrl over to the final Form. Any other overloads on BeginForm will go through a helper utility which will strip the query string.

    0 讨论(0)
  • 2021-01-01 10:21

    Just incase you wanted to add other attributes as well. use below code

    @using (Html.BeginForm("actionName", "controllerName", routeValues: new { lang = "en" }, method:FormMethod.Post, htmlAttributes: new { @class= "my-form", enctype = "multipart/form-data" }))
    
    0 讨论(0)
  • 2021-01-01 10:30

    I guess this doesn't directly answer the question, but why not just use a plain old form tag?

     <form action='customer/login?ReturnUrl=@Request.QueryString["ReturnUrl"]' method="post" data-id="something">
    

    Alternatively, you can create a custom HtmlHelperExtension that renders a form with path and querystring. In this HtmlHelperExtension you can iterate through your querystring values and populate the routeValueDictionary which you then pass to a Html.BeginForm constructor.

    If you don't want something so extensible you can just use the overloaded constructor of Html.BeginForm using @Html.BeginForm("login", "customer", new {ReturnUrl = @Request.QueryString["ReturnUrl"]},FormMethod.Post, new {data-id="something"});

    0 讨论(0)
  • 2021-01-01 10:36

    To create a RouteValueDictionary from the querystring:

    RouteValueDictionary queryStringDictionary = new RouteValueDictionary(Request.QueryString.AllKeys.ToDictionary(key => key, key => (object)Request.QueryString[key]));
    

    Then you can use it with Html.BeginForm:

    Html.BeginForm(null, null, queryStringDictionary, FormMethod.Post, new Dictionary<string, object> { { "autocomplete", "off" } })
    
    0 讨论(0)
提交回复
热议问题