代码之家  ›  专栏  ›  技术社区  ›  Fenton

在ASP.NETMVC中,窗体应该发布到自身还是其他操作?

  •  0
  • Fenton  · 技术社区  · 16 年前

    1贴到自己

    using (Html.BeginForm) {
        ...
    }
    

    在你的控制器里

        [HttpGet]
        public ActionResult Edit(int id)
    
        [HttpPost]
        public ActionResult Edit(EditModel model)
    

    在您使用的视图中

    using (Html.BeginForm("Save", "ControllerName")) {
    

    在你的控制器里

        [HttpGet]
        public ActionResult Edit(int id)
    
        [HttpPost]
        public ActionResult Save(EditModel model)
    

    摘要

    我可以看到每种方法的好处,前者提供了一种更为restful的风格,将相同的地址与正确的HTTP动词(GET、POST、PUT、DELETE等)结合使用。后者有一个URL模式,使得每个地址都非常具体。

    3 回复  |  直到 16 年前
        1
  •  4
  •   Darin Dimitrov    16 年前

    对于RESTful控制器:

    // return an HTML form for editing a specific entity
    public ActionResult Edit(int id) { }
    
    // find and update a specific entity
    [HttpPut]
    public ActionResult Update(EditModel userView) { }
    

    <% using (Html.BeginForm<HomeController>(c => c.Update(null))) {%>
        <%: Html.HttpMethodOverride(HttpVerbs.Put) %>
        <%: Html.EditorForModel() %>
        <input type="submit" value="Save" />
    <% } %>
    
        2
  •  3
  •   Mathias F    16 年前

    发布到同一操作。

        3
  •  0
  •   David Glenn    16 年前

    some good suggestions for standard action names 使用那个惯例。

    顺便说一句,如果REST是您的一项要求,那么我相信以下几点对于RESTfull应用程序更为正确:

    [HttpGet]
    public ActionResult Customer(int id) {
      //return customer details
    }
    
    [HttpPut]
    public ActionResult Customer(Customer cust) {
      //update customer
    }
    
    [HttpPost]
    public ActionResult Customer(Customer cust) {
      //insert new customer
    }