【问题标题】:MVC 4 Form - how the binding to the controller function is done?MVC 4 Form - 如何绑定到控制器功能?
【发布时间】:2013-09-16 12:30:44
【问题描述】:

我是 MVC 4 的新手(以及一般的 GUI),并且做了一两个教程,并获得了一些传统的 HTML 来将它们集成到 MVC 新解决方案中。

我的问题 -

我在 Models 文件夹中有一个 MyModel.cs。 我的 Views 文件夹中有 SignUp.cshtml(静态传统 html 没有 al HTML helpers..) 我的 Controllers 文件夹中有 MyController.cs。

  1. 我添加了对模型的引用 (@model XXX.XXX.MyModel)
  2. 在现有表单之前我添加了@using (Html.BeginForm())...
  3. 我将其中一个文本字段转换为新的方式:

    @Html.LabelFor(m => m.FirstName) @Html.TextBoxFor(m => m.FirstName)

现在呢?如何将表单详细信息传递给控制器​​? 尝试过

[HttpGet]
        public ActionResult Create()...

 [HttpPost]
        public ActionResult Create(MyModel model1)

但是在我提交表单后这些函数没有被调用...我在这里错过了一些东西(可能在我对绑定的理解中)

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 html-helper


    【解决方案1】:

    1) 如果您没有使用 HTML Helpers,请确保您的 View 中每个 input 控件的每个名称strong> 匹配模型中的属性名称,否则默认 ModelBinder 将无法在服务器中重新构建模型;

    MyModel.cs:

    public class MyModel
    {
       public string Property1 {get; set;}
       public string Property2 {get; set;}
    }
    

    HTML:

    <input type="text" name="Property1" id="Property1" />
    <input type="text" name="Property2" id="Property2" />
    

    2)你必须给Html.BeginForm提供action和其他参数,看来是你的问题,form找不到要发布的action,所以:

    @using(BeginForm("actionName", "controllerName", FormMethod /*POST or GET*/))
    {
       //Forms controls here
    }
    

    【讨论】:

    • 谢谢,听起来我很接近但仍然得到 /MyController/Create 的 404。我确实喜欢您的建议,并将 .cshtml 中的名称和 ID 与 Model 类中的属性名称相匹配。 “CREATE”代表要调用的方法名称吗?而“ControllerName”是controller.cs 类?
    • 您必须提供ControllerName 没有Controller,您如何调用BeginForm?也用视图代码更新答案。
    • BINGO(我删除了控制器后缀)!!!总是小事对吧?谢谢! @Jaimin - 我很快就会探索你的
    【解决方案2】:

    试试这个,

    型号

     public class RegisterModel
        {
            public int ID { get; set; }
    
    
            [Required(ErrorMessage = "Name is required.")]
            public string Name { get; set; }
    
            [Required(ErrorMessage = "Phone is required.")]
            public string PhoneNo { get; set; }
    
            [Required(ErrorMessage = "Address is required.")]
            public string Address { get; set; }
    }
    

    查看

    @model XXX.XXX.RegisterModel
    
    
      @using (Html.BeginForm("Create", "Test", FormMethod.Post, new { id = "FrmIndex" }))
            { 
               <div>
    
                    @Html.LabelFor(m => m.Name)
                    @Html.TextBoxFor(m => m.Name)
                    @Html.ValidationMessageFor(m => m.Name)
    
                </div>
                <br />
                <div>
                    @Html.LabelFor(m => m.Address)
                    @Html.TextBoxFor(m => m.Address)
                    @Html.ValidationMessageFor(m => m.Address)
                </div>
                <br />
                <div>
                    @Html.LabelFor(m => m.PhoneNo)
                    @Html.TextBoxFor(m => m.PhoneNo)
                    @Html.ValidationMessageFor(m => m.PhoneNo)
                  </div>
     <input type="submit" value="Save" style="float: left;" id="btnSave" title="btn" name="ButtonType" />
    }
    

    控制器

     [HttpGet]
         public ActionResult Create(RegisterModel model)
        {
             return View();
        }
         [HttpPost]
         public ActionResult Create(RegisterModel model)
        {
          if (model != null && ModelState.IsValid) // check to see if any users are missing required fields. if not...
            {
                if (model.ID != 0)
                {
                    UpdateRegister(model);
                }
                else
                {
    
                    InsertRegister(model);
                }
    
            }
            else
            {
                return View();
            }
        return RedirectToAction("Index");
        }
    

    【讨论】:

    • 谢谢!尝试过并在请求的 URL 上收到 404 错误:/MyController/Create。我需要创建一个新视图吗?如果我只想做一些内部数据库更新并返回主页怎么办?我应该更改您建议的参数吗?我认为 Create 是方法名称引用..
    • 您想将文本框 FirstName 值传递给控制器​​吗?
    • 嗨,我只是用示例更新我的代码,尝试这样,它完美地工作。
    【解决方案3】:

    第一次不需要传递模型,因为它只是一个空结构,在视图中引用:

        [HttpGet]
         public ActionResult Create()
        {
             return View();
        }
    
         [HttpPost]
         public ActionResult Create(RegisterModel model)
        {
          if (model != null && ModelState.IsValid) // check to see if any users are missing required fields. if not...
            {
                if (model.ID != 0)
                {
                    UpdateRegister(model);
                }
                else
                {
    
                    InsertRegister(model);
                }
    
            }
            else
            {
                return View();
            }
        return RedirectToAction("Index");
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-07
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      相关资源
      最近更新 更多