ASP.NET MVC核心标记帮助程序问题 [英] ASP.NET MVC Core Tag Helper Issue

查看:54
本文介绍了ASP.NET MVC核心标记帮助程序问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在表单元素中使用标签帮助程序时遇到问题.当我选择"GET" HTTP方法时,"hello"参数将不会填充Item控制器中用于Edit方法的参数.但是,当我选择"POST" HTTP方法时,参数将正确填充为"hello".为什么会这样?

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>

这是控制器:

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

推荐答案

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

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>

不能保证通过 action 方法属性发送现有的查询字符串值.请看

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

这篇关于ASP.NET MVC核心标记帮助程序问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆