【问题标题】:How fix Compiler Error Message: CS1061 in Asp.net MVC?如何修复 Asp.net MVC 中的编译器错误消息:CS1061?
【发布时间】:2016-04-01 09:21:49
【问题描述】:

我正在使用 ASP.net MVC 5Razor Engine。我想创建一个页面来注册用户。我为我的用户实体创建了一个模型和元数据模型。我所有的代码都在这里。我认为一切都很好,但我得到了这个编译错误:

Compiler Error Message: CS1061: 'NP1.ViewModels.RegisterVM' does not contain a definition for 'UserEmail' and no extension method 'UserEmail' accepting a first argument of type 'NP1.ViewModels.RegisterVM' could be found (are you missing a using directive or an assembly reference?)

谁能帮我解决一下?

控制器

[HttpGet]
    public ActionResult Register()
    {
        RegisterVM vm = new RegisterVM();
        vm.UserAddress = "";
        vm.UserBirthDate = DateTime.Now;
        vm.UserCellPhone = "";
        vm.UserEmail = "";
        vm.UserFirstName = "";
        vm.UserGender = "";
        vm.UserID = 1;
        vm.UserImage = "";
        vm.UserLastName = "";
        vm.UserPassWord = "";
        vm.UserStatus = 1;
        vm.UserTell = "";
        return View(vm);
    }

注册虚拟机

     public List<SocialNetwork> SocialNetworks { get; set; }
     public List<Footer> Footers { get; set; }
     public List<FooterMenu> FooterMenus { get; set; }
     public List<User> Users { get; set; }

Register.cs

    @model NP1.ViewModels.RegisterVM

    @{
      ViewBag.Title = "Register";
     }

  @using (Html.BeginForm()) 
   {
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>User</h4>
    <hr />
    @Html.ValidationSummary(true)

    <div class="form-group">
        @Html.LabelFor(model => model.UserEmail, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserEmail)
            @Html.ValidationMessageFor(model => model.UserEmail)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserFirstName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserFirstName)
            @Html.ValidationMessageFor(model => model.UserFirstName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserLastName, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserLastName)
            @Html.ValidationMessageFor(model => model.UserLastName)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserPassWord, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserPassWord)
            @Html.ValidationMessageFor(model => model.UserPassWord)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserCellPhone, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserCellPhone)
            @Html.ValidationMessageFor(model => model.UserCellPhone)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserTell, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserTell)
            @Html.ValidationMessageFor(model => model.UserTell)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserImage, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserImage)
            @Html.ValidationMessageFor(model => model.UserImage)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserAddress, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserAddress)
            @Html.ValidationMessageFor(model => model.UserAddress)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserStatus, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserStatus)
            @Html.ValidationMessageFor(model => model.UserStatus)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserBirthDate, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserBirthDate)
            @Html.ValidationMessageFor(model => model.UserBirthDate)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserGender, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.UserGender)
            @Html.ValidationMessageFor(model => model.UserGender)
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>
 }

  <div>
    @Html.ActionLink("Back to List", "Index")
  </div>

【问题讨论】:

    标签: asp.net-mvc razor model compiler-errors asp.net-mvc-5


    【解决方案1】:

    您需要在您的*.cshtml 中使用YourNameSpace。 像这样:@using MyNameSpace.

    answer 可能会有所帮助。

    【讨论】:

      【解决方案2】:

      错误是不言自明的。

      您的 razor 视图被强类型化为 RegisterVM ,但它没有名为 UserEmail 的属性。在您的视图中,您正在尝试使用模型的 UserEmail 属性。

      因此,使用视图所需的属性更新您的 RegisterVm 视图模型。

      public class RegisterVM
      {
        public string UserName {set;get;}
        public string UserFirstName {set;get;}
        public string UserLastName {set;get;}
        //Add other properties needed for the view.
      }
      

      或者

      看起来UserMetaData 类具有视图所需的所有属性。因此,将您的模型类型类更改为UserMetaData。在这种情况下,您需要将此类的可见性从 internal 更改为 public

      @model UserMetaData
      @using(Html.BeginForm())
      {
        @Html.TextBoxFor(s=>s.UserEmail)
        <input type="submit" />
      }
      

      【讨论】:

      • 谢谢,我知道了。 RegisterVm 是一个 ViewModel 。我将用户模型(具有所有属性)放在RegisterVm 中,但我不知道为什么会出现此错误。我认为这个错误没有理由:/ .
      • 理想情况下,您应该将缺少的属性添加到 RegisterVm 并使用它。
      • 我向RegisterVM 添加了属性,现在,我得到Object reference not set to an instance of an object `@foreach(Model.Footers 中的变量项)的错误...`。
      • 对不起,您的第二个解决方案,我的意思是,将internal 更改为public 并没有破坏metaDatamain model 之间的关系?
      • 由于您正在访问 Footers 属性,您应该将其添加到 registervm 并在您的 get 操作方法中初始化该属性。尝试第一个解决方案。
      猜你喜欢
      • 2016-06-30
      • 1970-01-01
      • 2011-10-07
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多