如何在Asp.net MVC中修复编译器错误消息:CS1061?

时间:2015-12-27 06:11:07

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

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

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

RegisterVM

     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>

2 个答案:

答案 0 :(得分:1)

错误是不言自明的。

您的剃刀视图强烈输入Intent i = new Intent(ChestActivity.this, TrackActivity.class); i.putExtra("CameFrom", "chest"); startActivity(i); ,但它没有名为RegisterVM的属性。在您的视图中,您尝试使用模型的UserEmail属性。< / p>

因此,请使用视图所需的属性更新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更改为public

internal

答案 1 :(得分:1)

您需要在app.use(bodyParser.json()); app.post('/', function(req, res, next) { console.log(req); res.send('s'); }); app.listen(3000); 中使用YourNameSpace。 像这样:*.cshtml

answer可能会有所帮助。