问题描述
可能重复:结果
从操作
我在我提交表单,并根据我想重定向到一个动作的结果的看法。这correspondes并采取行动的看法是强类型的,它应该接受ResultsViewModel。
I have a view in which I submit a form and depending on the results I want to redirect to an action. The view that correspondes to and action is strongly-typed and it should accept a ResultsViewModel.
我试图通过使用T4MVC的ResultsViewModel。
I'm trying to pass the ResultsViewModel using T4MVC.
下面是code:
[HttpPost]
public virtual ActionResult AddEntity(string viewModel)
{
//Deserialize using Json.NET
var entity = JsonConvert.DeserializeObject<MyEntity>(viewModel);
var success = DoSomething(); //returns boolean
if(success)
{
var result = new ResultsViewModel { MyEntity = entity, MessageId = 1};
return RedirectToAction(MVC.MyController.ResultsPage(result));
}
var result = new ResultsViewModel { MyEntity = entity, MessageId = 2};
return RedirectToAction(MVC.MyController.ResultsPage(result));
}
public virtual ActionResult ResultsPage(ResultsViewModel viewModel)
{
return View(viewModel);
}
当code到达
public virtual ActionResult ResultsPage(ResultsViewModel viewModel)
{
return View(viewModel);
}
视图模型总是等于空。
viewModel is always equal to null.
我知道我可以做这样的事情:
I know I can do something like this:
return RedirectToAction("ResultsPage", new { viewModel = result });
编辑:我试图返回RedirectToAction(ResultsPage,新的{视图模型=结果});而我也在我的ViewModel得到一个空。
I tried the return RedirectToAction("ResultsPage", new { viewModel = result }); and I'm also getting a null in my viewModel.
不过我想找出为什么/如何使用T4MVC传递一个对象。
However I'm trying to figure out why/how to pass an object using T4MVC.
谢谢,
推荐答案
使用的TempData code>
[HttpPost]
public virtual ActionResult AddEntity(string viewModel)
{
//Deserialize using Json.NET
var entity = JsonConvert.DeserializeObject<MyEntity>(viewModel);
var success = DoSomething(); //returns boolean
if(success)
{
var result = new ResultsViewModel { MyEntity = entity, MessageId = 1};
return RedirectToAction(MVC.MyController.ResultsPage(result));
}
var result = new ResultsViewModel { MyEntity = entity, MessageId = 2};
TempData["Result"] = result;
return RedirectToAction(MVC.MyController.ResultsPage(result));
}
public virtual ActionResult ResultsPage()
{
ResultsViewModel model = (ResultsViewModel)TempData["Result"];
return View(model);
}
这篇关于传入RedirectToAction)使用T4MVC一个对象(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!