Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 05, 2016 @ 15:58
    Gonzalo Matias Borghi
    1

    Issue with custom SurfaceController

    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

    enter image description here

    In Visual Studio

    Umbraco CMS (NuGet) 7.4.2
    
    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 (When I try to compile the solution)

    enter image description here

    Second question

    How do I filter children by custom bool property? "Poll" DocumentType have a "Active" bool property.

    This is correct?

    foreach (var currentPoll in testPage.Where("Active")) { ... }
    

    THX!!!!

  • Gonzalo Matias Borghi 10 posts 92 karma points
    Apr 06, 2016 @ 12:50
    Gonzalo Matias Borghi
    1

    I fixed the problem, conflict was giving the name of the solution, but now have a new problem. It is very common, but can not find the solution.

    enter image description here

  • Paul Sørensen 304 posts 650 karma points
    Apr 15, 2016 @ 21:43
    Paul Sørensen
    0

    Hi

    Quick fix for this is to upgrade to 7.4.3. Had a similar problem with RenderModel

    /Paul S

  • VanDred 6 posts 77 karma points
    Apr 15, 2016 @ 15:27
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies