在ASP.NET MVC中使用POST传递变量 [英] Passing variables with POST in ASP.NET MVC
问题描述
我正在尝试在asp.net MVC中传递一个字符串变量。我使用断点,所以我看到它确实转到控制器中的正确方法,但发布的变量等于null。
I am trying to pass a string variable inside asp.net MVC. I use breakpoints so I see that it does go to the correct method in the controller, but the variables posted are equal to null.
我的标记:
@{
ViewBag.Title = "TestForm";
}
<h2>TestForm</h2>
@using (Html.BeginForm()) {
<input type="text" id="testinput" />
<input type="submit" value="TestForm" />
}
我的控制器:
public ActionResult TestForm()
{
return View();
}
[HttpPost]
public ActionResult TestForm(string testinput)
{
Response.Write("[" + testinput + "]");
return View();
}
我把断点放在第二个TestForm方法中,testinput为null ... 。
我错过了什么吗?
I put the breakpoint inside the second TestForm method and testinput is null.... Am I missing something?
注意:我意识到大多数时候我会使用模型传递数据,但我想知道我也可以传递字符串。
Note: I realize that most of the time I will be using the model to pass data around, but I would like to know that I can pass strings as well.
作为同一问题的一部分,我如何传递几个变量?我的控制器中的方法是这样的:
As part of the same question, how do I pass several variables? Would the method in my controller look like this:
[HttpPost]
public ActionResult TestForm(string var1, var2)
{
}
推荐答案
我看起来你设置的id不是名字。我每天都使用MVC3,所以我不会重现你的样本。 (我醒了20个小时编程;)但仍然有动力去帮助)请告诉我它是否不起作用。但对我来说,看起来你必须设置name属性...而不是id属性。试试......我现在正等着帮助你,如果它不起作用。
For me it looks like that you set the id not the name. I use MVC3 every day, so i dont reproduce your sample. (I am awake for 20 hours programming ;) but still motivated to help ) Please tell me if it dont work. But for me it looks like you have to set the "name" property ... not the id property. Try that ... i am waiting right now to help you if it does not work.
<input type="text" id="testinput" name="testinput" />
这篇关于在ASP.NET MVC中使用POST传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!