4

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)

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

enter image description here

EDIT

New problem enter image description here

avechuche
  • 1,470
  • 6
  • 28
  • 45

2 Answers2

1

From your error You are getting a namespace conflict with your Polls namespace and the Umbraco.Web.PublishedContentModels.Polls namespace. Either change you namespaces to match that of Umbraco (NOT RECOMMENDED) or try changing your namespaces to something unique that will not conflict with Umbraco's namespaces.

Something like: MyRootNamespace.Polls where you would replace MyRootNamespace with your own custom namespace.

SurfaceController

namespace MyRootNamespace.Polls.Controllers {
    public class PollsController : SurfaceController {...}
}

Models

namespace MyRootNamespace.Polls.Models {
    public class Answer {...}

    public class PollViewModel {...}
}

PartialView "Polls"

@model IEnumerable<MyRootNamespace.Polls.Models.PollViewModel>

<div>
    ...
</div>
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

The Umbraco.Web.PublishedContentModels namespace is were you can finde a new feature of Umbraco, caled the Model Builder. If enabled, you can get strongly typed models for each of your document types to use within your views.

Based on the error message, Umbraco is trying to use the Polls model generated from your document type, resulting in a conflict with your view model.

Unless you intend to use these autogenerated models, I suggest you disable the feature by adding or updating this line in the web.config file:

<add key="Umbraco.ModelsBuilder.Enable" value="false" />

It's not clear which reference exactly is causing the issue, but other things you could try are renaming your view model or investingating any parent views, for example if your view has a layout.

elolos
  • 4,310
  • 2
  • 28
  • 40