Reputation: 27
In my application there are two text boxes and one button, I want to get the text to the controller when user clicks the button.
I have tried HttpPost but it does not work, here is my code:
View
@model examplemvc1.Models.sample
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.TextBoxFor(a=>a.username)
@Html.TextBoxFor(a=>a.password)
<input type="submit" value"button1" />
Controller
namespace examplemvc1.Controllers
{
public class sampleController : Controller
{
//
// GET: /sample/
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(sample sam)
{
return View(sam);
}
}
}
model
namespace examplemvc1.Models
{
public class sample
{
public string username { get; set; }
public string password { get; set; }
}
}
Upvotes: 0
Views: 196
Reputation: 4918
At the request of the OP:
@model examplemvc1.Models.sample
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
@Html.TextBoxFor(a=>a.username)
@Html.TextBoxFor(a=>a.password)
<input type="submit" value"button1" />
}
Upvotes: 1
Reputation: 15933
You have to add a form which will be posted to your controller
@using (Html.BeginForm())
{
@Html.TextBox("Name");
@Html.Password("Password");
<input type="submit" value="Sign In">
}
Produces the following form element
<form action="/Original Controller/Original Action" action="post">
Upvotes: 2