MVC 中复制的嵌套转发器行为
今天我决定尝试一下 MVC,虽然我真的很喜欢这个想法,但我发现从 ASP.NET 过渡并掌握一些基本概念相当困难,例如使用 foreach 而不是嵌套重复器。
我花了好几个小时才想出这个解决方案,但它似乎不太正确。有人可以解释一下这段代码有什么问题,以及正确的方法是什么。这是我的解决方案:
本质上,这是一项由多个问题组成的调查,每个问题都有多个答案。我在数据库中有表,它们表示为强类型实体。控制器看起来像这样:
public ActionResult Details(int id)
{
return View(new Models.Entities().Questions.Where(r => r.PROMId == id));
}
相应的视图像这样:
<% foreach (var question in Model) { %>
<h3>Question <%: Array.IndexOf(Model.ToArray(), question) + 1 %></h3>
<p><%: question.QuestionPart1 %></p>
<p><%: question.QuestionPart2 %></p>
<% var answers = new Surveys_MVC.Models.Entities().Answers.Where(r => r.QuestionId == question.QuestionId); %>
<% foreach (var answer in answers) { %>
<input type="radio" /><%: answer.Text %>
<% } %>
<% } %>
感谢所有反馈。
Today I decided to give MVC a go, and although I really like the idea, I found it fairly difficult to transition from ASP.NET and grasp some basic concepts, like using foreach instead of nested repeaters.
It took me good few hours to come up with this solution, but it doesn't seem quite right. Could someone please explain what's wrong with this code, and what the right way to do it is. Here is my solution:
Essentially it's a survey that consists of several questions, each of which has several answers. I have tables in db, which are represented as strongly typed entities. The controller looks like this:
public ActionResult Details(int id)
{
return View(new Models.Entities().Questions.Where(r => r.PROMId == id));
}
and corresponding view like this:
<% foreach (var question in Model) { %>
<h3>Question <%: Array.IndexOf(Model.ToArray(), question) + 1 %></h3>
<p><%: question.QuestionPart1 %></p>
<p><%: question.QuestionPart2 %></p>
<% var answers = new Surveys_MVC.Models.Entities().Answers.Where(r => r.QuestionId == question.QuestionId); %>
<% foreach (var answer in answers) { %>
<input type="radio" /><%: answer.Text %>
<% } %>
<% } %>
All feedback appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
至于使用
for
循环来实现嵌套转发器行为,我认为这是在 MVC 中执行此操作的最佳方法。但我建议您使用专用的 ViewModel。ViewModel:
Controller:
View:
这种方法有一些优点:
for
循环中执行Array.IndexOf(Model.ToArray(), Question)
和数据库往返,如果您这样做,这可能会变得相当昂贵页面上有多个问题。当然,您的单选按钮需要具有与其关联的输入名称和值,否则在提交表单时您将无法检索此信息。通过让控制器决定如何生成输入名称,您可以更清楚地了解
Details
方法与SaveAnswers
方法的对应关系。以下是
GetRadioQuestionListModelById
的可能实现:As far as using
for
loops for the nested repeater behavior, I think that's the best way to do this in MVC. But I would suggest you use dedicated ViewModels.ViewModel:
Controller:
View:
This approach has a few advantages:
Array.IndexOf(Model.ToArray(), question)
and a database roundtrip inside afor
loop, which can become pretty costly if you have more than a few questions on the page.And of course your radio buttons need to have a input name and value associated with them, or you'll have no way to retrieve this information when the form is submitted. By making the controller decide how the input name gets generated, you make it more obvious how the
Details
method corresponds to yourSaveAnswers
method.Here's a possible implementation of
GetRadioQuestionListModelById
:我不知道这是否更好,但是您可以创建一个助手来为您执行此操作:
然后您可以像这样调用它:
这具有很大的功能,上面只是一个一般示例。您可以在此处阅读更多信息 http: //haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx
I don't know if it is better, but you can create a helper to do this for you:
Then you can call it like so:
This has a lot of power and the above is just a general example. You can read more here http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx