gpt4 book ai didi

c# - 如何在 ASP.NET MVC 4 中通过 POST 传递值

转载 作者:太空狗 更新时间:2023-10-29 18:13:18 26 4
gpt4 key购买 nike

我在 ASP.NET MVC 4 中通过 POST 传递值时遇到问题这是我在用户 Controller 中的操作:

[HttpPost]
public string Show(int? uid, string uname)
{
return uname + uid.ToString();
}

这就是我尝试在 View 中传递值的方式:

@using (Html.BeginForm("Show", "User"))
{
Html.Hidden("uid", Model.Id);
Html.Hidden("uname", Model.UserName);
<input type="submit" value="+"/>
}

html:

 <form action="/User/Show" method="post"> <input type="submit" value="+"/> </form>

和:

@using(Html.BeginForm("Show", "User", FormMethod.Post, new { uid = 1, uname = "user1" }))
{
<input type="submit" value="+"/>
}

html:

<form action="/User/Show" method="post" uid="1" uname="user1"> <input type="submit" value="+"/></form>

在这两种方式中,Show 操作都会接收 null 而不是实际值。

最佳答案

您的 HtmlHelpers 没有被渲染。使用 Razor 语法。

@using (Html.BeginForm("Show", "User"))
{
@Html.Hidden("uid", Model.Id);
@Html.Hidden("uname", Model.UserName);
<input type="submit" value="+"/>
}

解释:

调用Html.Hidden(或Html.[anything])是一个方法,通常返回一个IHtmlString。如果不使用 @ infront,引擎不知道您正在尝试输出返回的字符串。它只是认为您正在调用一个方法。

关于c# - 如何在 ASP.NET MVC 4 中通过 POST 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21840894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com