[英]How to get textbox value from view to controller in mvc4 on submit button click
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. 如何在mvc4中从视图到控制器获取文本框值?如果我在控制器中使用httppost方法,则找不到页面错误。
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???**
}
First you need to change your button type to "submit" . 首先,您需要将按钮类型更改为“提交” 。 so your form values will be submitted to your Action method.
所以您的表单值将提交给您的Action方法。
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. 其次,您需要在Action方法中将模型添加为参数。
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var strname=model.Empname;
return View();
}
Third, If your Controller name is "FormController". 第三,如果您的Controller名称是“FormController”。 you need to change the parameter of your Html.Beginform in your view to this:
您需要在视图中将Html.Beginform的参数更改为:
@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))
{
//your fields
}
PS 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. PS如果您的视图与Action方法的名称相同,即“DisplayForm”,则无需在Html.BeginForm中添加任何参数。 just to make it simple.
只是为了简单。 like so:
像这样:
@using (Html.BeginForm())
{
//your fields
}
Have an ActionResult
for the form post: 为表单发布一个
ActionResult
:
[HttpPost]
public ActionResult DisplayForm(FormModel formModel)
{
//do stuff with the formModel
frmmdl.Empname = formModel.Empname;
}
Look into Model Binding . 看看模型绑定 。 Default model binding will take the data embedded in your posted form values and create an object from them.
默认模型绑定将获取已发布表单值中嵌入的数据,并从中创建对象。
Let's implement simple ASP.NET MVC subscription form with email textbox. 让我们用电子邮件文本框实现简单的ASP.NET MVC订阅表单。
The data from the form is mapped to this model 表单中的数据将映射到此模型
public class SubscribeModel
{
[Required]
public string Email { get; set; }
}
View name should match controller method name. 视图名称应与控制器方法名称匹配。
@model App.Models.SubscribeModel
@using (Html.BeginForm("Subscribe", "Home", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<button type="submit">Subscribe</button>
}
Controller is responsible for request processing and returning proper response view. Controller负责请求处理并返回正确的响应视图。
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Subscribe(SubscribeModel model)
{
if (ModelState.IsValid)
{
//TODO: SubscribeUser(model.Email);
}
return View("Index", model);
}
}
Here is my project structure. 这是我的项目结构。 Please notice, "Home" views folder matches HomeController name.
请注意,“Home”视图文件夹与HomeController名称匹配。
You model will be posted as a object on the action and you can get it in action on post like this: 您的模型将作为对象发布到操作上,您可以在帖子中将其设置为以下操作:
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
// do whatever needed
string emp = model.EmpName;
}
it you are posting data always put HttpPost attribute on the action. 你发布数据总是把HttpPost属性放在动作上。
Your view also has mistakes, make it like this: 你的观点也有错误,就像这样:
@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))
{
<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>
}
Model values from hidden field? 隐藏字段的模型值? I recommend the strongly typed approach shown below:
我推荐如下所示的强类型方法:
public ActionResult DisplayForm(string Emplname, string Emplid, string Empldepart)
There are two ways you can do this. 有两种方法可以做到这一点。
The first uses TryUpdateModel
: 第一个使用
TryUpdateModel
:
public ActionResult DisplayForm()
{
FormModel frmmdl = new FormModel();
TryUpdateModel (frmmdl);
// Your model should now be populated
}
The other, simpler, version is simply to have the model as a parameter on the [HttpPost]
version of the action: 另一个更简单的版本只是将模型作为动作的
[HttpPost]
版本的参数:
[HttpPost]
public ActionResult DisplayForm(FormModel frmmdl)
{
// Your model should now be populated
}
Change your controller like below. 如下更改您的控制器。
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var Empname = model.Empname;
}
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
FormModel frmmdl = new FormModel();
frmmdl.Empname=**How to get the textbox value here from view on submitbutton //click???**
}
model.Empname will have the value model.Empname将具有该值
You need to have both Get and Post Methods: 您需要同时拥有Get和Post方法:
[HttpGet]
public ActionResult DisplayForm()
{
FormModel model=new FormModel();
return View(model);
}
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var employeeName=model.Empname;
return View();
}
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
var value1 = model.EmpName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.