first of all, I want to clarify that I'm just learning ASP MVC and Umbraco :). There may be very novice mistakes. I'm trying to make a system of interviews. Searching the Internet, I found an example I changed my taste. I created an empty project for testing, and this is all I have.
Umbraco admin
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 content
In Visual Studio
Umbraco CMS (NuGet)
Controllers
PollController
Models
PollViewModel
Answer
SurfaceController
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;
}
}
}
Model
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 "Polls"
@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>
Error
EDIT