【发布时间】:2016-04-05 14:14:44
【问题描述】:
首先,我想澄清一下,我只是在学习 ASP MVC 和 Umbraco :)。可能会有非常新手的错误。 我正在尝试建立一个面试系统。在网上搜索,我找到了一个改变我口味的例子。 我创建了一个空项目进行测试,这就是我所拥有的。
Umbraco 管理员
DocumentTypes
Answer // --> Without template (for the moment) - No have properties (for the moment)
Poll // --> Without template (for the moment) - No have properties (for the moment) - Have many "Answer" child and "Active" bool property
Polls // --> Without template (for the moment) - No have properties (for the moment) - Have many "Poll" child
PartialView
Polls
Umbraco 内容
在 Visual Studio 中
Umbraco CMS (NuGet)
Controllers
PollController
Models
PollViewModel
Answer
表面控制器
namespace Polls.Controllers
{
public class PollsController : SurfaceController
{
[HttpPost]
public ActionResult Submit(PollViewModel model)
{
// Do something...
return RedirectToCurrentUmbracoPage();
}
public ActionResult Index()
{
var testPage = Umbraco.Content(CurrentPage.Id);
var questions = new List<PollViewModel>();
foreach (var currentPoll in testPage.Where("Active"))
{
questions.Add(new PollViewModel { ID = currentPoll.ID, Title = currentPoll.Name.ToString(), Answers = AnswerList(currentPoll.ID) });
}
return PartialView("~/Views/Polls.cshtml", questions);
}
private List<Answer> AnswerList(int myQuestionID)
{
var questionPage = Umbraco.Content(myQuestionID);
var answers = new List<Answer>();
foreach (var currentAnswer in questionPage.Children)
{
answers.Add(new Answer { ID = currentAnswer.ID, Text = currentAnswer.Name.ToString() });
}
return answers;
}
}
}
型号
namespace Polls.Models
{
public class Answer
{
public int ID { get; set; }
public string Text { get; set; } // --> No use for now
}
}
namespace Polls.Models
{
public class PollViewModel
{
public int ID { get; set; }
public string Title { get; set; } // --> No use for now
public List<Answer> Answers { get; set; }
}
}
PartialView“投票”
@model IEnumerable<Polls.Models.PollViewModel>
<div>
@using (Html.BeginUmbracoForm<Polls.Controllers.PollsController>("Submit"))
{
foreach (var item in Model)
{
<div>
@Html.Hidden(item.ID.ToString())
<p>
<strong>@item.Title</strong>
</p>
@{
foreach (var answerItem in item.Answers)
{
<div>
@Html.RadioButton(item.Title, answerItem.ID, new { @id = answerItem.ID })
@Html.Label(answerItem.Text, new { @for = answerItem.ID })
</div>
}
}
</div>
}
<div>
<button type="submit">Send...</button>
</div>
}
</div>
错误
编辑
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 umbraco7