c# - 模型命名冲突导致 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 中保留所有模型的唯一名称,还是有更好的解决方案?提前致谢!
解决方案
你的错误说明了一切。
仅仅因为它们看起来相同,并不意味着它们是相同的。编译器比这更聪明,会将 2 个单独的类视为2 个单独的类。
使用相同的模型。或根据需要使用界面
推荐阅读
- python - TypeError: __init__() 得到了一个意外的关键字参数“madataset_iter”
- batch-file - 如何将正确的日文字符显示为批处理文件的输出?
- visual-studio-code - 我可以从 VSC 中的终端中删除该位置吗?
- scope - 如何访问 request.user nestjs 微服务?
- sql-server - tsql 中的 xml 节点() - 将父节点与子节点属性匹配
- linux - 更改目录不是通过脚本工作,而是直接在机器上工作
- reactjs - 使用 Helmet 看不到源代码上的元标记
- cookies - Safari 上 Outlook 加载项中的用户会话
- jquery - 如何使用 Jquery 从 Ajax 实时搜索返回响应?
- javascript - 有没有办法确定 SVG TextPath 的哪一部分被剪裁了