ASP.NET MVC Core Tag Helper Issue

*爱你&永不变心* 提交于 2021-02-08 10:25:18

问题


I am having an issue using tag helpers in a form element. When I choose the "GET" HTTP method, the parameter for my Edit method in my Item controller won't get filled by the "hello" argument. However, when I choose the "POST" HTTP method, the parameter is filled correctly with "hello". Why is this happening?

<form asp-controller="Item" asp-action="Edit" asp-route-item="hello" method="get">
    <input type="submit" />
</form>

And here is the controller:

    [HttpGet]
    [HttpPost]
    public IActionResult Edit(string item)
    {
        if (Request.Method == "GET")
        {
            ViewData["item"] = item;
            return View();
        }
    }

回答1:


Nothing to do with the form tag helper. When using HTML form tag, if your are using GET method, the browser will read the form element values and append that to the form action attribute value url following a ?. So i assume your browser is removing your existing query string items for this purpose. So consider moving that to an input element inside the form.

<form asp-controller="Home" asp-action="Edit" method="get">
    <input type="hidden" name="item" value="second"/>
    <input type="submit"/>
</form>

Sending the existing querystring values from the action method attribute is not guaranteed to work.. Take look at submitting a GET form with query string params and hidden params disappear



来源:https://stackoverflow.com/questions/50892106/asp-net-mvc-core-tag-helper-issue

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