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
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" />
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:
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>