如何在提交按钮单击时在mvc4中从视图到控制器获取文本框值

时间:2022-12-06 21:46:34

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???**
        }

10 个解决方案

#1


12  

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". you need to change the parameter of your Html.Beginform in your view to this:

第三,如果您的Controller名称是“FormController”。您需要在视图中将Html.Beginform的参数更改为:

@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:

附:如果您的视图与Action方法的名称相同,即“DisplayForm”,则无需在Html.BeginForm中添加任何参数。只是为了简单。像这样:

@using (Html.BeginForm())
{
//your fields
}

#2


3  

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.

看看模型绑定。默认模型绑定将获取已发布表单值中嵌入的数据,并从中创建对象。

#3


1  

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>

}

#4


1  

Let's implement simple ASP.NET MVC subscription form with email textbox.

让我们用电子邮件文本框实现简单的ASP.NET MVC订阅表单。

Model

The data from the form is mapped to this model

表单中的数据将映射到此模型

public class SubscribeModel
{
    [Required]
    public string Email { get; set; }
}

View

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

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名称匹配。

如何在提交按钮单击时在mvc4中从视图到控制器获取文本框值

#5


0  

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
    }

#6


0  

Change your controller like below.

如下更改您的控制器。

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
   var Empname = model.Empname;
}

#7


0  

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();
    }

#8


0  

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
    var value1 = model.EmpName;
}

#9


0  

Model values from hidden field? I recommend the strongly typed approach shown below:

隐藏字段的模型值?我推荐如下所示的强类型方法:

public ActionResult DisplayForm(string Emplname, string Emplid, string Empldepart)

#10


0  

[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将具有该值

#1


12  

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". you need to change the parameter of your Html.Beginform in your view to this:

第三,如果您的Controller名称是“FormController”。您需要在视图中将Html.Beginform的参数更改为:

@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:

附:如果您的视图与Action方法的名称相同,即“DisplayForm”,则无需在Html.BeginForm中添加任何参数。只是为了简单。像这样:

@using (Html.BeginForm())
{
//your fields
}

#2


3  

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.

看看模型绑定。默认模型绑定将获取已发布表单值中嵌入的数据,并从中创建对象。

#3


1  

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>

}

#4


1  

Let's implement simple ASP.NET MVC subscription form with email textbox.

让我们用电子邮件文本框实现简单的ASP.NET MVC订阅表单。

Model

The data from the form is mapped to this model

表单中的数据将映射到此模型

public class SubscribeModel
{
    [Required]
    public string Email { get; set; }
}

View

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

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名称匹配。

如何在提交按钮单击时在mvc4中从视图到控制器获取文本框值

#5


0  

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
    }

#6


0  

Change your controller like below.

如下更改您的控制器。

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
   var Empname = model.Empname;
}

#7


0  

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();
    }

#8


0  

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
    var value1 = model.EmpName;
}

#9


0  

Model values from hidden field? I recommend the strongly typed approach shown below:

隐藏字段的模型值?我推荐如下所示的强类型方法:

public ActionResult DisplayForm(string Emplname, string Emplid, string Empldepart)

#10


0  

[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将具有该值