c# – 自定义表面控制器Umbraco问题

首先,我想澄清一点,我只是在学习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的内容

《c# – 自定义表面控制器Umbraco问题》

在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;
        }

    }
}

模型

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# – 自定义表面控制器Umbraco问题》

编辑

新问题
《c# – 自定义表面控制器Umbraco问题》

最佳答案 从您的错误您获得与Polls名称空间和Umbraco.Web.PublishedContentModels.Polls名称空间的名称空间冲突.您可以更改名称空间以匹配Umbraco(不推荐),也可以尝试将名称空间更改为与Umbraco名称空间不冲突的唯一名称空间.

类似于:MyRootNamespace.Polls,您将使用自己的自定义命名空间替换MyRootNamespace.

SurfaceController

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

楷模

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

    public class PollViewModel {...}
}

PartialView“民意调查”

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

<div>
    ...
</div>
点赞