控制器和视图之间传递数据 - ASP.NET MVC 4 [英] Passing data between controller and view - ASP.NET MVC 4
问题描述
我想提出一个简单的调查,为ASP.NET MVC的学习基础4.这里是我的code
I am making a simple survey, for learning basics of ASP.NET MVC 4. Here's my code
[HttpGet]
public ActionResult Index()
{
UserAndTableViewmodel Viewmodel = new UserAndTableViewmodel();
Viewmodel.T = Deserialize();
Viewmodel.U = new User();
for (int i = 0; i < Viewmodel.T.Questions.Count(); i++)
{
Viewmodel.U.UserChoices.Add(new Choice(Viewmodel.T.Questions[i].Choices[0].Value));
}
return View(Viewmodel);
}
[HttpPost]
public ActionResult Index(UserAndTableViewmodel Viewmodel)
{
// Viewmodel.T = Deserialize();
if (ModelState.IsValid)
{
return View("Thanks", Viewmodel);
}
else
{
return View(Viewmodel);
}
}
中的XML code是如下:
The XML code is as followed:
<Table>
<Question Content="Question one">
<Choice Value="Answer 1" />
<Choice Value="Answer 2" />
</Question>
<Question Content="Question two">
(...)
</Question>
</Table>
我传递deserialised数据索引的观点,在这里用户可以选择自己的答案。然后数据被发布到[HttpPost]我希望它渲染视图,其中其回答每一个问题时写的,但出现的问题 - Viewmodel.T
等于null 。什么是我应该做的,我不应该再反序列化呢?
I'm passing deserialised data to "Index" view, where user can choose his answers. Then data is post to [HttpPost] and i want it to render a view, where each question with its answer is written, but problem occurs - Viewmodel.T
is equal to null. What am I supposed to do, that I shouldn't deserialize it again?
推荐答案
您不想做任何MVC4或连载deserialise。只是通过您的数据视图模型对象,将在您的视图可用。
You don't want to do any serialize or deserialise in MVC4. Just pass your data as a view model object, that will be available in your view.
这篇关于控制器和视图之间传递数据 - ASP.NET MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!