【问题标题】:How to get textbox value from view to controller in mvc4 on submit button click如何在提交按钮单击时从视图获取文本框值到 mvc4 中的控制器
【发布时间】:2014-05-20 19:20:30
【问题描述】:

如何在 mvc4 中从视图获取文本框值到控制器?如果我在控制器中使用 httppost 方法,页面找不到错误。

查看

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

型号

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

控制器

        public ActionResult DisplayForm()
        {
            FormModel frmmdl = new FormModel();
            frmmdl.Empname=**How to get the textbox value here from view on submitbutton click???**
        }

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4


【解决方案1】:

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

型号

表单中的数据映射到这个模型

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

查看

视图名称应与控制器方法名称匹配。

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

控制器

控制器负责处理请求并返回正确的响应视图。

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

这是我的项目结构。请注意,“Home”视图文件夹与 HomeController 名称匹配。

【讨论】:

    【解决方案2】:

    首先您需要将按钮类型更改为“提交”。因此您的表单值将提交给您的 Action 方法。

    来自:

    <input type="button" id="submitId" value="submit" />
    

    到:

    <input type="submit" id="submitId" value="submit" />
    

    其次,您需要将模型作为参数添加到 Action 方法中。

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

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

    @using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))
    
        {
        //your fields
        }
    

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

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

    【讨论】:

      【解决方案3】:

      您的模型将作为一个对象发布在操作上,您可以像这样在发布时将其投入使用:

      [HttpPost]
      public ActionResult DisplayForm(FormModel model)
              {
                  // do whatever needed
                string emp = model.EmpName; 
              }
      

      如果您要发布数据,请始终将 HttpPost 属性放在操作上。

      你的观点也有错误,改成这样:

      @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】:
        [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 将具有值

        【讨论】:

          【解决方案5】:

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

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

          【讨论】:

            【解决方案6】:

            有一个ActionResult 用于表单帖子:

            [HttpPost]
            public ActionResult DisplayForm(FormModel formModel)
            {
                //do stuff with the formModel
                frmmdl.Empname = formModel.Empname;
            }
            

            查看Model Binding。默认模型绑定将获取嵌入在您发布的表单值中的数据并从中创建一个对象。

            【讨论】:

              【解决方案7】:
              [HttpPost]
              public ActionResult DisplayForm(FormModel model)
              {
                  var value1 = model.EmpName;
              }
              

              【讨论】:

                【解决方案8】:

                你需要同时拥有 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();
                    }
                

                【讨论】:

                  【解决方案9】:

                  如下更改您的控制器。

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

                  【讨论】:

                    【解决方案10】:

                    有两种方法可以做到这一点。

                    第一次使用TryUpdateModel

                        public ActionResult DisplayForm()
                        {
                            FormModel frmmdl = new FormModel();
                            TryUpdateModel (frmmdl);
                    
                            // Your model should now be populated
                        }
                    

                    另一个更简单的版本是简单地将模型作为[HttpPost] 版本动作的参数:

                        [HttpPost]
                        public ActionResult DisplayForm(FormModel frmmdl)
                        {
                            // Your model should now be populated
                        }
                    

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 2019-08-22
                      • 1970-01-01
                      • 2018-07-12
                      • 1970-01-01
                      • 1970-01-01
                      • 2020-12-26
                      • 2012-11-04
                      • 1970-01-01
                      相关资源
                      最近更新 更多