什么是FormCollection参数?这是一个虚假的论点吗?

时间:2010-12-20 17:07:40

标签: asp.net-mvc

到目前为止,我不知道为什么VS默认提供FormCollection参数?

    public ActionResult Edit(int id)
    {
        Dinner dinner = dinnerRepository.GetDinnerById(id);

        if (dinner == null)
            return View("NotFound");
        else
            return View(dinner);
    }

    [HttpPost]
    public ActionResult Edit(int id, object dummy/*, FormCollection collection*/)
    {
        Dinner temp = dinnerRepository.GetDinnerById(id);

        if (TryUpdateModel(temp))
        {
            dinnerRepository.Save();

            return RedirectToAction("Details", new { id = temp.DinnerId });
        }
        else
            return View(temp);
    }

编辑1:在我的实验中,除id之外的任何参数都是虚拟的,因为它们从不在httppost编辑操作方法中使用。

编辑2:TryUpdateModel是否在场景后面使用FormCollection?

3 个答案:

答案 0 :(得分:3)

如果您的应用收到了POST,则99.99%的时间来自HTML表单。 FormsCollection为您收集表单中的所有值。

在ASP.NET MVC中,使用强类型对象几乎总是更好。 DefaultModelBinder会在大多数时间为您创建它们,如果默认值不能满足需要,您可以实施IModelBinder

答案 1 :(得分:2)

FormCollection包含您回到服务器的表单状态。

如果您需要对数据处理进行任何自定义操作,请使用FormCollection。否则你可以高兴地删除它。

我正在大量使用它来进行分层模型处理。

答案 2 :(得分:2)

FormCollection是ASP.NET如何让您访问刚刚发布到页面的值。

您还可以使用强类型参数,然后ASP.NET MVC将在内部使用FormCollection来创建强类型对象。

相关问题