Umbraco - 定制模型导致问题?

时间:2017-06-08 05:59:53

标签: asp.net-mvc model-view-controller umbraco umbraco7

在一个香草Umbraco项目中,我正在导入一个vanilla MVC应用程序。 由于这个错误,我现在正担心:

Cannot bind source type eParser.Model.MyModel to model type MyNamespace.Controllers.ViewModel`1[[eParser.Model.MyModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

这是我的代码:

本地项目

namespace MyNamespace.Controllers
{
    public class MyOutputController : RenderMvcController
    {
        private static Parser _parser;

        public MyOutputController()
        {
            _parser = new Parser(@"localfile.txt");
        }

        public override ActionResult Index(RenderModel model)
        {
        var settings = new RPSettings();
        var wsParser = _parser.GetwsParser();
            var result = wsParser.GetDataAsMyModel(settings);
        return View("MyView", result);
    }
     }

    public class ViewModel<TModel> : RenderModel
    {
        public ViewModel(RenderModel model) : base(model.Content, model.CurrentCulture) { }

        public TModel MyModel { get; set; }
    }
}

外部项目

namespace eParser
{
    public class wsParser
    {
    public MyModel GetDataAsMyModel(RPSettings settings)
        {
            var model = new MyModel();
        return model;
    }
    }
}

namespace eParser.Model
{
    public class MyModel
    {
        public string Name { get; set; }
        public string Value1 { get; set; }
        public bool IsValid { get; set; }
    }
}

模板

@using MyNamespace.Controllers
@using OtherNamespace.Models
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ViewModel<MyModel>>
@{
    Layout = "/Views/Shared/_Layout.cshtml"; //Just <html> and very basic - no code
    var addModel = new AddListViewModel
    {
        MyType = MyType.Value1,
        MyModel = Model
    };
}

<h1>@Model.Name</h1>


namespace OtherNamespace.Models
{
    public class AddListViewModel
    {
        public string ValueName { get; set; }
        public MyType MyType { get; set; }
        public ViewModel<MyModel> MyModel { get; set; }
    }
}

我有什么想法可以解决它?

1 个答案:

答案 0 :(得分:0)

您的Index方法错误 - 您将模型传递给视图,但您应该传递视图模型。

应该是:

public override ActionResult Index(RenderModel model)
{
    var settings = new RPSettings();
    var wsParser = _parser.GetwsParser();
    var result = wsParser.GetDataAsMyModel(settings);

    var viewModel = new ViewModel<MyModel>(model);
    viewModel.MyModel = result;

    return View("MyView", viewModel );
}