HTML Form: why action can't have get value in it?

前端 未结 7 2261
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-19 12:07

When i try the following structure, it does\'t send id=value

7条回答
  •  情歌与酒
    2020-12-19 12:36

    This behaviour is really annoying. One workaround is to drop the form completely and build your url by hand on the client side.

    I used jQuery in one of our .NET projects to achieve this. It is a search mask with dozens of parameters. One of them is a range slider (min/max) values. You use the slider to select two values and have to press on the search button next to it.

    
    
    @(Model.Unit) to @(Model.Unit)

    And the event handler for the button:

    UrlHelper h = UrlHelperExtensions.CreateRequestAgnosticUrlHelper();
    
    /* Search-Button Click-EventHandler */
    $("#@(sliderButton)").click(function(){
         @{
          string url = h.Action("Index", "SearchAction", new RouteValueDictionary(searchParams));
          string urlSeparator = url.Contains("?") ? "&" : "?";
         }
         var url = '@Html.Raw(url + urlSeparator + sliderInfo.AssociatedFieldName)=' + $("#@Html.Raw(sliderMinId)").val() + '%20-%20' + $("#@Html.Raw(sliderMaxId)").val();
         window.location.replace(url);
    });
    

    This is what happens:

    1. Build an Url containing all previous parameters.
    2. Append your new "form" parameters to it. In this case they are added as one parameter (min-max). Don't let that confuse you.
    3. Redirect to the new Url.

    This is totally awkward for something that could be so simple... I hope this helps.

提交回复
热议问题