代码之家  ›  专栏  ›  技术社区  ›  Dave Mateer

是否应在不正常状态下创建对象?从创建控制器传递到视图

  •  1
  • Dave Mateer  · 技术社区  · 14 年前

    从控制器传递一个空白对象如客户端有什么好处?

    public ActionResult Create()
            {
                Client client = new Client();
                return View(client);
            }
    
            //
            // POST: /Client/Create
            [HttpPost]
            public ActionResult Create(Client clientToAdd)
            {
                try
                {
                    clientRepository.Insert(clientToAdd);
                    return RedirectToAction("Index");
    

     public ActionResult Create()
            {
                return View();
            }
    
            //
            // POST: /Client/Create
            [HttpPost]
            public ActionResult Create(Client clientToAdd)
            {
                try
                {
                    clientRepository.Insert(clientToAdd);
                    return RedirectToAction("Index");
    

    干杯

    1 回复  |  直到 14 年前
        1
  •  0
  •   John Farrell    14 年前

    这种技术还提供了方便的默认值,就像其他回答者所说的那样,允许您在简单场景中重用添加/编辑视图。