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

典型的ASP.NET MVC-控制器操作名称约定是什么?

  •  0
  • bzarah  · 技术社区  · 14 年前

    我正在努力为自己的行为选择合适的名字。我想区分返回视图的操作:

    1. 创造
    2. 编辑
    3. 编辑创建

    以下是迄今为止我所拥有的:

    Create --> Show the Empty Form for Create  
    Add    --> Receives data from Create and Save New Entity  
    Edit   --> Shows Existing Entity in a form for editing  
    Update --> Saves the changes on an existing Entity  
    ???    --> Shows the form for either editing or creating depending on the 
               situation  
    Save   --> Either saves or updates the entity depending on whether the entity 
               already exists or not.  
    

    那么,对于显示 Create/Edit 视图,将其数据发送到 Save .

    我考虑过 CreateEdit 因为它是明确和具体的,但我不确定。有什么建议吗?

    1 回复  |  直到 14 年前
        1
  •  4
  •   Mike Scott    14 年前

    我通常使用 Create() 用于添加新实体和 Edit() 编辑一个。

    我重载get&post方法并添加 [HttpPost] 属性到接收数据的人,例如:

    public ActionResult Create()
    {
        // this one renders the input form
        ...
        return View();
    }
    
    [HttpPost]
    public ActionResult Create( MyViewModel model )
    {
        // this one accepts the form post
        if ( ModelState.IsValid )
        {
           ...
           return RedirectToAction( ... );
        }
        return View( model );
    }
    

    这种惯例使找到相关方法变得容易。