ASP.NET MVC核心标记助手问题

时间:2018-06-16 21:58:43

标签: asp.net-mvc asp.net-core-mvc asp.net-mvc-controller

我在表单元素中使用标记帮助器时遇到问题。当我选择" GET" HTTP方法,我的项目控制器中的编辑方法的参数不会被" hello"填充。论点。但是,当我选择" POST" HTTP方法,参数用" hello"正确填充。为什么会这样?

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

这是控制器:

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

1 个答案:

答案 0 :(得分:0)

form标记助手无关。使用HTML form代码时,如果您使用的是GET方法,则浏览器会读取表单元素值,并将其附加到action后的?属性值url表单中。因此,我假设您的浏览器正在删除现有的查询字符串项目。因此,请考虑将其移动到表单内的输入元素。

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

无法保证从action方法属性发送现有的查询字符串值。请查看submitting a GET form with query string params and hidden params disappear

相关问题