9

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.

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?

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 Answers 2

20

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" />
Sign up to request clarification or add additional context in comments.

7 Comments

yep it worked!hmmmm. why did MS decide to rely on a name and not ID????why do they always do it backwards???? But thanks!
No, Microsoft do really great with mvc, dont think about microsoft about that. Its about web-standards. Basicly the id property is used to apply javascript (getElementsById, CSS-StyleSheets, jQuery and more). The name property is relevant for posts and async (data-driven) things.
@sarsnake - this has nothing to do with Microsoft. The browser is posting the values to the server. The name field is used in html forms and always has been. The reason is that you can only have one item with any given ID, but several things can have the same name. This makes it easy to create lists of items. Don't complain about Microsoft for something that you don't actually understand.
the last sentence could have been omitted. You are rude.
Couldn't figure this out all morning and you helped me out. Thanks for this dknaack!
|
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:

  • strongly-typed views
  • MVC Model Binding
  • Html Helpers

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>

2 Comments

If you are preaching about strongly typed views, why would you rely on the formCollection in your POST action method. Shouldn't it be public ActionResult TestForm(TestModel model)?
I pass a FormCollection like this for the purposes of unit testing without mocks. My test can pass a form collection to the UpdateModel call and have it map the values onto the TestModel model object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.