Asp.net mvc 要传回httppost的泛型对象/类类型?

Asp.net mvc 要传回httppost的泛型对象/类类型?,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我有一个方法可以从我的帖子中的视图返回我的viewmodel: [HttpPost] public ActionResult DoStuff(daViewModel model) { string whatever = model.Name; int id = model.Id; return View(); } 将什么类型的对象get传递回post上的控制器方法(我的viewmodel是否包装在类的httppost类型中?)是否有可以传递的泛型/类型,例如: [Htt

我有一个方法可以从我的帖子中的视图返回我的viewmodel:

[HttpPost]
public ActionResult DoStuff(daViewModel model)
{
    string whatever = model.Name;
    int id = model.Id;
    return View();
}
将什么类型的对象get传递回post上的控制器方法(我的viewmodel是否包装在类的httppost类型中?)是否有可以传递的泛型/类型,例如:

[HttpPost]
public ActionResult DownloadFiles(object model)
{
    // cast my daViewModel from object model as passed in???
    string whatever = model.Name;
    int id = model.Id;
    return View();
}

您可以传递
FormCollection
对象:

[HttpPost]
public ActionResult DownloadFiles(FormCollection collection)
{
    // if you want to extract properties directly:
    string whatever = collection["Name"];
    int id = int.Parse(collection["Id"]);

    // if you want to convert the collection to your model:
    SomeModel model;
    TryUpdateModel(model, collection);

    return View();
}
TryUpdateModel
方法返回一个布尔值。如果它成功地更新了模型,它将返回true,否则它将返回false。传入的表单值应与模型的属性名称匹配

如果您在调用
return View()
时询问返回的模型是什么,那么答案是什么,除非您告诉它。
View()
方法中有一个重载,该方法接受一个模型:

return View(model);

您应该返回视图期望看到的类型。如果将视图定义为具有
Foo
模型,则最好在控制器中返回
Foo

您可以传递
FormCollection
对象:

[HttpPost]
public ActionResult DownloadFiles(FormCollection collection)
{
    // if you want to extract properties directly:
    string whatever = collection["Name"];
    int id = int.Parse(collection["Id"]);

    // if you want to convert the collection to your model:
    SomeModel model;
    TryUpdateModel(model, collection);

    return View();
}
TryUpdateModel
方法返回一个布尔值。如果它成功地更新了模型,它将返回true,否则它将返回false。传入的表单值应与模型的属性名称匹配

如果您在调用
return View()
时询问返回的模型是什么,那么答案是什么,除非您告诉它。
View()
方法中有一个重载,该方法接受一个模型:

return View(model);

您应该返回视图期望看到的类型。如果您将视图定义为具有
Foo
模型,那么最好在控制器中返回
Foo

我不太明白您的问题,但是您的类的数据类型是由您定义的,ModelBinder将表单值或查询字符串中的数据绑定到您的模型类。您可以创建自己的模型绑定器类来处理特定的模型。我不太明白您的问题,但是您的类的数据类型是由您定义的,ModelBinder将表单值或查询字符串中的数据绑定到您的模型类。您可以创建自己的模型绑定器类来处理特定的模型。Thx!如果视图模型中有一个复杂的对象集合,该怎么办?我可数肌球蛋白?我仍然可以将其映射到[“MyObject”]并从表单集合中相应地获取该集合吗?此外,它是否会映射到TryUpdateModel?@Mariah如果表单数据命名正确,它将能够更新可枚举(或列表/数组等)。Thx!如果视图模型中有一个复杂的对象集合,该怎么办?我可数肌球蛋白?我仍然可以将其映射到[“MyObject”]并从表单集合中相应地获取该集合吗?此外,它是否会映射到TryUpdateModel?@Mariah如果表单数据命名正确,它将能够很好地更新可枚举(或列表/数组等)。