2

I'm using ASP.net MVC 5 with Razor Engine. I want to create a page to register users . I create a Model and MetaData model for my User entity . All my codes are here . I think everything is Ok but I got this compile error :

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?)

Could anyone help me how fix it,please ?

controller

[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>
shm
  • 439
  • 2
  • 11
  • 25

2 Answers2

2

You need to using YourNameSpace in your *.cshtml. Like this:@using MyNameSpace.

This answer can be helpful.

Community
  • 1
  • 1
1

The error is self explanatory.

Your razor view is strongly typed to RegisterVM , but it does not have a property called UserEmail.In your view you are trying to use the UserEmail property of your model.

So update your RegisterVm view model with the properties needed for the view.

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.
}

Or

Looks like UserMetaData class has all the properties you needed for your view. So change your model type class to UserMetaData. In that case, you need to change the visibility of this class to public from internal.

@model UserMetaData
@using(Html.BeginForm())
{
  @Html.TextBoxFor(s=>s.UserEmail)
  <input type="submit" />
}
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Thank you , I know . `RegisterVm` is a ViewModel . I put User Model (with all properties ) in `RegisterVm` and I don't know why I got this error. I think there is no reason for this error :/ . – shm Dec 27 '15 at 06:19
  • Ideally you should add the missing properties to RegisterVm and use that. – Shyju Dec 27 '15 at 06:24
  • I added properties to `RegisterVM` and now , I got `Object reference not set to an instance of an object` error for ` @foreach (var item in Model.Footers)...` . – shm Dec 27 '15 at 06:31
  • excuse me , your second solution , I mean , change `internal` to `public` didn't break relation between `metaData` and `main model ` ? – shm Dec 27 '15 at 06:37
  • Since you are accessing the Footers property , you should add that to the registervm and initialize that property in your get action method. Try the first solution. – Shyju Dec 27 '15 at 06:58
  • As you said , I added all properties . but I'm confused how I initialize that , Could you please tell me a example ? , sorry – shm Dec 27 '15 at 07:07
  • Looks like your view is not using any of those collection properties(Footers,SocialNetworks,FooterMenu etc..). You should remove those from your view model. Where are you getting the errors ? – Shyju Dec 27 '15 at 16:16
  • No , I'm using those properties in Layout . when I remove Layout, Register page works correctly but I need those models in Layout. You said initialize that properties , I don't know how I do it because I'm new in mvc :( . I used ViewModels before and they worked correctly I don't know why this didn't work. – shm Dec 28 '15 at 04:17
  • You may initialize that in your class constructor to empty list. Also you may need to load the real data (non-empty list) in your GET action. Take a look at this link http://bit.ly/1Ogo0ZI – Shyju Dec 28 '15 at 04:23
  • Excuse me , you mean that I should change my `RegisterVM` to yours (your link)? I changed it , but I got same error :( . Really I don't want initialize properties with unappropriate data , I just want to create a Register page to add users and show some other links fetched from DB – shm Dec 28 '15 at 04:35
  • So initialize it in your GET action method. – Shyju Dec 28 '15 at 04:36
  • Excuse me again , you mean some codes like this : [link](https://gist.github.com/anonymous/20ef67d469b8adda35b5) , I did it but it gave me same error for Footer – shm Dec 28 '15 at 05:19
  • I still don't you initializing the Footers collecction ! – Shyju Dec 28 '15 at 05:24
  • dear , with these codes ` this.Footers = new List
    (); this.FooterMenus = new List(); this.SocialNetworks = new List();` that error (null model) solved , but it shows empty footer. I want to show real data fetched from db , could you plz help me again ?
    – shm Dec 28 '15 at 07:07
  • You need to load the real footer collection in your get action. Now, I don't know your db structure or your data access code to tell you how to do that. What you need to do is writing some code to get a list of Footer item and assign that to your Footers property of the RegisterVm view model object. – Shyju Dec 28 '15 at 13:40