在ASP中使用POST传递变量。NET MVC

时间:2022-11-30 17:33:31

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.

我试图在asp.net MVC中传递一个字符串变量。我使用了断点,所以我看到它确实指向了控制器中的正确方法,但是所张贴的变量等于null。

My markup:

我的标记:

@{
    ViewBag.Title = "TestForm";
}

<h2>TestForm</h2>

@using (Html.BeginForm()) {
   <input type="text" id="testinput" />

    <input type="submit" value="TestForm" />
}

My controller:

我的控制器:

public ActionResult TestForm()
{
    return View();
} 

[HttpPost]
public ActionResult TestForm(string testinput)
{
    Response.Write("[" + testinput + "]");

    return View();
}

I put the breakpoint inside the second TestForm method and testinput is null.... Am I missing something?

我把断点放在第二个TestForm方法和testinput是null ....我遗漏了什么东西?

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)
{
}

2 个解决方案

#1


18  

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.

对我来说,你好像设置了id而不是名字。我每天都用MVC3,所以我不复制你的样品。(我已经20个小时没睡了;)但是我仍然有动力去帮助你。但对我来说,你必须设置"name"属性…而不是id属性。试一试……如果不行,我现在正等着帮你。

     <input type="text" id="testinput" name="testinput" />

#2


1  

On a slightly separate note there is nothing wrong with passing variables like you are, but a more efficient way would be to pass around a strongly typed view model allowing you to take advantage of many aspects of MVC's goodness:

在一个稍微分开的注释中,像您这样传递变量并没有什么错,但是更有效的方法是传递一个强类型的视图模型,这样您就可以利用MVC的许多优点:

  • strongly-typed views
  • 强类型的视图
  • MVC Model Binding
  • MVC模型绑定
  • Html Helpers
  • Html帮助

Create a new view model:

创建一个新的视图模型:

public class TestModel
{
    public string TestInput { get; set; }
}

Your test controller:

您的测试控制器:

    [HttpGet]
    public ActionResult TestForm()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TestForm(FormCollection collection)
    {
        var model = new TestModel();
        TryUpdateModel(model, collection);

        Response.Write("[" + model.TestInput + "]");

        return View();
    }

Your view:

你的观点:

@model <yourproject>.Models.TestModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>TestForm</title>
</head>
<body>
    <div>
        @using(Html.BeginForm())
        {
            <div class="editor-label">
                @Html.LabelFor(m => m.TestInput)
            </div>
            <div class="editor-label">
                @Html.TextBoxFor(m => m.TestInput)
            </div>
            <input type="submit" value="Test Form"/>
        }
    </div>
</body>
</html>

#1


18  

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.

对我来说,你好像设置了id而不是名字。我每天都用MVC3,所以我不复制你的样品。(我已经20个小时没睡了;)但是我仍然有动力去帮助你。但对我来说,你必须设置"name"属性…而不是id属性。试一试……如果不行,我现在正等着帮你。

     <input type="text" id="testinput" name="testinput" />

#2


1  

On a slightly separate note there is nothing wrong with passing variables like you are, but a more efficient way would be to pass around a strongly typed view model allowing you to take advantage of many aspects of MVC's goodness:

在一个稍微分开的注释中,像您这样传递变量并没有什么错,但是更有效的方法是传递一个强类型的视图模型,这样您就可以利用MVC的许多优点:

  • strongly-typed views
  • 强类型的视图
  • MVC Model Binding
  • MVC模型绑定
  • Html Helpers
  • Html帮助

Create a new view model:

创建一个新的视图模型:

public class TestModel
{
    public string TestInput { get; set; }
}

Your test controller:

您的测试控制器:

    [HttpGet]
    public ActionResult TestForm()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TestForm(FormCollection collection)
    {
        var model = new TestModel();
        TryUpdateModel(model, collection);

        Response.Write("[" + model.TestInput + "]");

        return View();
    }

Your view:

你的观点:

@model <yourproject>.Models.TestModel

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>TestForm</title>
</head>
<body>
    <div>
        @using(Html.BeginForm())
        {
            <div class="editor-label">
                @Html.LabelFor(m => m.TestInput)
            </div>
            <div class="editor-label">
                @Html.TextBoxFor(m => m.TestInput)
            </div>
            <input type="submit" value="Test Form"/>
        }
    </div>
</body>
</html>