【问题标题】:Issue with custom surface controller Umbraco自定义表面控制器 Umbraco 的问题
【发布时间】: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


    【解决方案1】:

    根据您的错误,您的 Polls 命名空间和 Umbraco.Web.PublishedContentModels.Polls 命名空间存在命名空间冲突。要么更改命名空间以匹配 Umbraco (不推荐),要么尝试将命名空间更改为不会与 Umbraco 命名空间冲突的独特名称。

    类似:MyRootNamespace.Polls,您可以将 MyRootNamespace 替换为您自己的自定义命名空间。

    表面控制器

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

    模型

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

    部分视图“投票”

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

    【讨论】:

    • 完美,但现在我有一个新问题。去编辑。这是一个常见问题(谷歌中很多),但我无法解决。
    • 检查关于禁用模型绑定器的第二个答案。
    【解决方案2】:

    Umbraco.Web.PublishedContentModels 命名空间是您可以找到 Umbraco 的一个新功能,称为模型生成器。如果启用,您可以获得每种文档类型的强类型模型,以在您的视图中使用。

    根据错误消息,Umbraco 正在尝试使用从您的文档类型生成的 Polls 模型,从而导致与您的视图模型发生冲突。

    除非您打算使用这些自动生成的模型,否则我建议您通过在 web.config 文件中添加或更新此行来禁用该功能:

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

    尚不清楚究竟是哪个引用导致了问题,但您可以尝试的其他方法是重命名您的视图模型或投资任何父视图,例如,如果您的视图具有布局。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多