How to get the textbox value from view to controller in mvc4?If I using httppost method in controller the page cannot found error was came.
View
@model MVC_2.Models.FormModel
@{
ViewBag.Title = "DisplayForm";
}
@using (Html.BeginForm("DisplayForm", "FormController", FormMethod.Post))
{
<form>
<div>
@Html.LabelFor(model => model.Empname)
@Html.TextBoxFor(model => model.Empname)
@* @Html.Hidden("Emplname", Model.Empname)*@
@Html.LabelFor(model => model.EmpId)
@Html.TextBoxFor(model => model.EmpId)
@* @Html.Hidden("Emplid", Model.EmpId)*@
@Html.LabelFor(model => model.EmpDepartment)
@Html.TextBoxFor(model => model.EmpDepartment)
@* @Html.Hidden("Empldepart", Model.EmpDepartment)*@
<input type="button" id="submitId" value="submit" />
</div>
</form>
}
model
public class FormModel
{
public string _EmpName;
public string _EmpId;
public string _EmpDepartment;
public string Empname
{
get {return _EmpName; }
set { _EmpName = value; }
}
public string EmpId
{
get { return _EmpId;}
set {_EmpId =value;}
}
public string EmpDepartment
{
get { return _EmpDepartment; }
set { _EmpDepartment = value; }
}
}
controller
public ActionResult DisplayForm()
{
FormModel frmmdl = new FormModel();
frmmdl.Empname=**How to get the textbox value here from view on submitbutton click???**
}
MVC will automatically take the value of UnboundTextBoxName and insert that value into the parameter of the same name. Show activity on this post. You should be using <form> tags and set the action to your controller and have set a place in your model to store the data.
You cannot directly pass XML data as file to MVC controller. One of the best method is to pass XML data as Stream with HTTP post.
First you need to change your button type to "submit". so your form values will be submitted to your Action method.
from:
<input type="button" id="submitId" value="submit" />
to:
<input type="submit" id="submitId" value="submit" />
Second you need to add your model as parameter in your Action method.
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var strname=model.Empname;
return View();
}
Third, If your Controller name is "FormController". you need to change the parameter of your Html.Beginform in your view to this:
@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))
{
//your fields
}
P.S. If your view is the same name as your Action method which is "DisplayForm" you don't need to add any parameter in the Html.BeginForm. just to make it simple. like so:
@using (Html.BeginForm())
{
//your fields
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With