【问题标题】:Error when going to a MVC view - The type or namespace name 'UserModel' does not exist in the namespace转到 MVC 视图时出错 - 名称空间中不存在类型或名称空间名称“UserModel”
【发布时间】:2015-02-13 23:55:56
【问题描述】:

当我在浏览器中转到视图用户/添加时,我收到以下消息 -

编译器错误消息:CS0234:命名空间“CRM.Core.Models”中不存在类型或命名空间名称“UserModel”(您是否缺少程序集引用?)

任何想法我哪里出错了?

查看

@model CRM.Core.Models.UserModel

@{
    ViewBag.Title = "Add";
}

<h2>Add</h2>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)

<fieldset>
    <legend>UserModel</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.EmployeeNumber)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EmployeeNumber)
        @Html.ValidationMessageFor(model => model.EmployeeNumber)
    </div>
</fieldset>
}

用户控制器

public ActionResult Add()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Add(UserModel model)
    {
        if (ModelState.IsValid)
        {
            _userService.Add(model);
        }

        return View(model);
    }

UserModel.cs

namespace CRM.Core.Models
{
    public class UserModel : IUserModel
    {
        [DisplayName("Employee Number")]
        public int EmployeeNumber { get; set; }
    }
}

【问题讨论】:

  • @using CRM.Core.Models; 添加到您的视图有帮助吗?
  • 刚刚试过了,还是不行
  • 假设项目已编译并且类是公共的...尝试使用global::CRM.Core.Models.UserModel,以防万一您有一些名称重复的命名空间...
  • 如果您的模型在单独的程序集中,请确保正确构建并正确引用它。
  • 所有似乎都被引用了 好的,并且已经完成了重建、清理等工作。@AdrianoRepetti 你的建议会去哪里?以前从未见过。

标签: c# asp.net-mvc


【解决方案1】:

不太清楚发生了什么,但我只是重新启动 PC 并再次重建。现在似乎一切正常。

【讨论】:

    【解决方案2】:

    在我的例子中,引用模型被编译成 exe(控制台应用程序),而 ASP.NET 不喜欢这种模型。 所以我不得不在不同的程序集中创建模型——(类库项目)并将其引用到视图中。

    这让它起作用了。

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 2011-02-24
      相关资源
      最近更新 更多