如何处理模型具有嵌套集合的强类型 MVC3 视图?

发布于 2024-11-16 16:38:27 字数 294 浏览 4 评论 0原文

我有一个模型,其中有“问题”集合。每个问题都有一个“可能答案”的集合。可能的答案对象具有 isAnswer 属性,该属性应绑定到代表问题的所选单选按钮(在每个问题的组中)。

我是 MVC 新手,真的不确定如何构建视图,以便发布的模型将根据关联单选按钮的选择,收集每个问题的可能答案,其中一个对象的 isAnswer 属性设置为 true团体。

现在,视图应该为每个问题构建一个单选按钮组/列表,其中问题的可能答案集合代表与该问题相关的单选按钮选项。我可以在剃刀中做嵌套循环吗?你使用部分吗?当我发布它时,MVC 如何知道如何根据视图重建模型?

i have a model that has a Collections of "Questions". Each Question has a collection of "PossibleAnswers". The possible answer objects have an isAnswer property which should be bound to the selected radio button (in the group for each question) that represents the question.

I am new to MVC and really not sure how to build the view so that the posted model will have the collection of possibleanswers for each question with one of the objects having the isAnswer property set as true, based on the selection from the associated radio button group.

Right now the view should build a radio button group/list for each question with the possibleanswer collection of the question representing a radio button choice related to the question. Can i do nested loop in razor? do you use partials? how does MVC know how to rebuild the model based on the view when i post it??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2024-11-23 16:38:27

其实很简单。棘手的部分是使用索引器

    namespace MvcApplication2.Controllers
{
    public class QuizModel
    {
        public IList<QuestionModel> Questions { get; set; }
    }
    public class QuestionModel
    {
        public IList<AnswerModel> PossibleAnswers { get; set; }       
    }
    public class AnswerModel
    {
        public bool IsAnswer { get; set; }
    }


    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(new QuizModel
                            {
                                Questions = Enumerable.Repeat(
                                    new QuestionModel
                                        {
                                            PossibleAnswers = Enumerable.Repeat(new AnswerModel(), 3).ToList()
                                        }, 2).ToList()
                            });
        }
        [HttpPost]
        public ActionResult Index(QuizModel model)
        {
            return View(model);
        }
    }}

然后你的视图

@model MvcApplication2.Controllers.QuizModel
@{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Index</h2>
    @using (Html.BeginForm())
    {
for (int i = 0; i < Model.Questions.Count; i++)
{
    for (int j = 0; j < Model.Questions[i].PossibleAnswers.Count; j++)
    {
    <div>
        @Html.EditorFor(c => Model.Questions[i].PossibleAnswers[j].IsAnswer)
    </div>
    }
}
<input type="submit" value="Submit" />
    }

It's actually very simple. The tricky part is to use indexers

    namespace MvcApplication2.Controllers
{
    public class QuizModel
    {
        public IList<QuestionModel> Questions { get; set; }
    }
    public class QuestionModel
    {
        public IList<AnswerModel> PossibleAnswers { get; set; }       
    }
    public class AnswerModel
    {
        public bool IsAnswer { get; set; }
    }


    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View(new QuizModel
                            {
                                Questions = Enumerable.Repeat(
                                    new QuestionModel
                                        {
                                            PossibleAnswers = Enumerable.Repeat(new AnswerModel(), 3).ToList()
                                        }, 2).ToList()
                            });
        }
        [HttpPost]
        public ActionResult Index(QuizModel model)
        {
            return View(model);
        }
    }}

Then your view

@model MvcApplication2.Controllers.QuizModel
@{
    View.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Index</h2>
    @using (Html.BeginForm())
    {
for (int i = 0; i < Model.Questions.Count; i++)
{
    for (int j = 0; j < Model.Questions[i].PossibleAnswers.Count; j++)
    {
    <div>
        @Html.EditorFor(c => Model.Questions[i].PossibleAnswers[j].IsAnswer)
    </div>
    }
}
<input type="submit" value="Submit" />
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文