首页 > 解决方案 > 模型命名冲突导致 InvalidOperationException

问题描述

我有一个 ASP.NET Core MVC 应用程序,它引用了两个 Razor 类库。在每个 RCL 中,都有一个同名的 Model 类(称为GeneralModel)。运行应用程序时会出现以下错误:

InvalidOperationException:传递到 ViewDataDictionary 的模型项的类型为“Namespace1.Models。GeneralModel ',但这个 ViewDataDictionary 实例需要一个类型为“Namespace2.Models”的模型项。通用模型'.

如果我将其中一个 RCL 中的模型之一重命名为唯一,则不会发生错误,并且应用程序会按预期执行。

我制作了一个简单的应用程序和两个简单​​的 RCL,以确保它不是我们真正的解决方案中的东西。错误再次发生。

这是代码:RCL 1 控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TestRCL1.Models;

namespace TestRCL1.Controllers
{
    public class TestRCL1Controller : Controller
    {
        public IActionResult Index()
        {
            try
            {
                GeneralModel generalModel = new GeneralModel();
                generalModel.fooBar = "fooBar from RCL1";

                return PartialView("_GeneralView", generalModel);
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
                return NotFound();
            }
        }
    }
}   

RCL 2 控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TestRCL2.Models;

namespace TestRCL2.Controllers
{
    public class TestRCL2Controller : Controller
    {
        public IActionResult Index()
        {
            try
            {
                GeneralModel generalModel = new GeneralModel();
                generalModel.fooBar = "fooBar from RCL2";

                return PartialView("_GeneralView", generalModel);
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
                return NotFound();
            }
        }
    }
}   

RCL 1 型号:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestRCL1.Models
{
    public class GeneralModel
    {
        public string fooBar { get; set; }
    }
}

RCL 2 型号:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestRCL2.Models
{
    public class GeneralModel
    {
        public string fooBar { get; set; }
    }
}

RCL 1 视图:

@model TestRCL1.Models.GeneralModel

<div>@Model.fooBar</div>

RCL 2 视图:

@model TestRCL2.Models.GeneralModel

<div>@Model.fooBar</div>

主应用的主控制器:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using TestRCLApp.Models;

namespace TestRCLApp.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

主应用的 Index.cshtml:

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>

    <button id="loadRCL1Content" type="button">Load RCL1 Content</button>
    <button id="loadRCL2Content" type="button">Load RCL2 Content</button>

</div>

<script>
    $('#loadRCL1Content').click(function (e) {
        $.ajax({
            url: '/TestRCL1/Index',
            type: 'POST'
        })
        .fail(function (req, status, error) {
            alert(error);
        })
        .done(function (responseData, status, responseObj) {
            $('#partialPlaceHolder').html(responseData);
        });
    });

    $('#loadRCL2Content').click(function (e) {
        $.ajax({
            url: '/TestRCL2/Index',
            type: 'POST'
        })
        .fail(function (req, status, error) {
            alert(error);
        })
        .done(function (responseData, status, responseObj) {
            $('#partialPlaceHolder').html(responseData);
        });
    });

</script>

测试应用的解决方案资源管理器屏幕截图

有没有其他人遇到过这个问题?我是否只需要在我的 RCL 中保留所有模型的唯一名称,还是有更好的解决方案?提前致谢!

标签: c#asp.net-mvcasp.net-core

解决方案


你的错误说明了一切。

仅仅因为它们看起来相同,并不意味着它们是相同的。编译器比这更聪明,会将 2 个单独的类视为2 个单独的类。

使用相同的模型。或根据需要使用界面


推荐阅读