开发者

Asp.net MVC 2: Understanding CRUD, specfically actions Create ( there are 2)?

Can anyone tell help me understand the new CRUD scaffolding that is included with MVC 2?

Let me explain, for example below you have 2 Create Actions...

Now i presume that if i have the form "Post to itself" then the second with Attribute POST is executed - IS THIS CORRECT? so a form within a view that when Submitted submits to itself??, but when would the standard Create be call开发者_运维知识库ed i.e. the 1 that has the // GET comment at the start.

I do understand that the default action is Index hence this would be normally called when my page is displayed but i can't seem to find any info on Create action. I presume its a magic word hence it needs to be called Create ???

    // GET: /Customer/Create

    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Customer/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }


Create() (no attributes) is called when the page is first loaded. ie. The empty form is displayed to the user

Create(FormCollection) (AcceptVerbs attribute) is called when the form is submitted with data.

Both can use the same ASPX as a view.


There's nothing magic about the name 'Create'. Any normal link to the create action (say, one created by a Url.Action("Create") call) would result in a page request to the non-POST (GET) version. A form on the GET version of the page with method="POST" will result in the POST version getting called. You can use this same pattern for actions with other names.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜