我無法將視圖模型傳遞到視圖中。我有兩個看法:一個Search
視圖和一個GeneralForm
視圖。 Search
將搜索結果傳遞給GeneralForm
的視圖模型。ViewModel在動作之間傳遞時變爲空
說出GeneralForm
是一個複雜的視圖模型持有兩個其他的ViewModels:
public class GeneralFormViewModel
{
public GeneralInfoViewModel GeneralInfo { get; set; }
public NotesViewModel Notes { get; set; }
}
public class GeneralInfoViewModel
{
[Required(ErrorMessage = "Please enter the person's name.")]
[DisplayName("Name:")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter the person's ID.")]
[DisplayName("ID:")]
public int ID { get; set; }
}
public class NotesViewModel
{ // etc.
(我成立了以我的GeneralForm視圖中使用多個@Html.BeginForm
s此方式通過這種方式,我希望爲POST和驗證整個一般形式,一個小室的時間,使用KnockoutJS和AJAX)
[HttpPost]
public ActionResult Search(SearchViewModel vm)
{
var query = // do some searching
var viewmodel = new GeneralFormViewModel()
{
GeneralInfo = new GeneralInformationViewModel
{
ID = query.id,
Name = query.name
}
};
return RedirectToAction("GeneralForm", viewmodel);
}
此時,viewmodel.GeneralInfo
不是空,並且t他將viewmodel傳遞給GeneralForm控制器。
[HttpGet]
public ActionResult GeneralForm(GeneralFormViewModel model)
{
return View(model);
}
現在model.GeneralInfo
是空。我通過這樣做打破了哪些MVC約定,以及如何獲得GeneralForm視圖以將通過搜索控制器獲取的數據呈現給GeneralForm視圖?
[Controller.RedirectToAction](http://msdn.microsoft.com/en-us/library/ system.web.mvc.controller.redirecttoaction(v = vs.118).aspx)沒有采用視圖模型的方法。 – 2014-10-10 16:06:39
[傳遞模型和參數與RedirectToAction]可能重複(http://stackoverflow.com/questions/16468061/passing-model-and-parameter-with-redirecttoaction) – 2014-10-10 16:07:06