Umbraco - custom model causing problems?

巧了我就是萌 提交于 2019-12-11 07:28:51

问题


In a vanilla Umbraco project, i am importing a vanilla MVC app. I am currently beeing held up, by this error:

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]].

This is my code:

Local project

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

External project

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

Template

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

Any ideas what i could do to fix it?


回答1:


Your Index method is wrong - you are passing in your model to the view but you should be passing your view model instead.

It should be:

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


来源:https://stackoverflow.com/questions/44427732/umbraco-custom-model-causing-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!