0

I am currently working on a project, I have 2 tables for now, User and Designation. When a user registers they will have to select a designation, which means that DesignationID is a FK in User table, however in my view, I do not want to display the ID I want it to display the name in a dropdown list that the user can select when registering. I have tried a few solutions however I am still getting an error which I'm not sure how to fix as I am new to asp.net , Please can you assist? Here is what I've tried

User model:

public partial class User
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public User user { get; set; }

    [Required]
    [Display(Name ="First Name")]
    public string FirstName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required]
    [Display(Name = "Designation")]
    public int DesignationID { get; set; }
    public virtual Designation designation { get; set; }

    [Required]
    [Display(Name = "Email")]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Password")]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    [Required]
    [Display(Name = "Address")]
    public string Address { get; set; }

    [Required]
    [Display(Name = "Date of birth")]
    [DataType(DataType.Date)]
    public System.DateTime DOB { get; set; }
}

Designation model:

public partial class Designation
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]

    public int DesignationID { get; set; }

    [Required]
    [Display(Name ="Designation Name")]
    public string DesignationName { get; set; }

    public virtual ICollection<User> users { get; set; }
}

Controller code I used

public class HomeController : Controller
{
    private CompanyPrintersEntities db = new CompanyPrintersEntities();
    public ActionResult Index()
    {  
        return View(db.Users.Include(us => us.user).Include(us => us.designation).ToList());
    }
}

View:

<div class="form-group">
           
 @Html.LabelFor(model => model.DesignationID, htmlAttributes: new { @class = "control-label col-md-2" })

    <div class="col-md-10">
        @foreach (User item in Model)
        {
            <p>User @item.User.Name is in Designation @item.Designation.Name</p>
        }
    </div>
</div>

Error I am getting:

Error

I am using Entity Framework DB first approach.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33
  • Can you show your `@model` statement from Register.cshtml ? Possibly you are referring to another class that is not the `User` class. – Yong Shun Sep 02 '21 at 09:03
  • I've tried quite a few: @model EngenPrintersApp.Models.User – Pravania Reddy Sep 02 '21 at 09:05
  • And also @model IEnumerable – Pravania Reddy Sep 02 '21 at 09:07
  • I only have one user class, do you think maybe it's a conflict of reference? – Pravania Reddy Sep 02 '21 at 09:10
  • Try import namespace with `@using EngenPrintersApp.Models` in cshtml to solve *type or namespace User could not be found.*. And use `@model EngenPrintersApp.Models.User`. – Yong Shun Sep 02 '21 at 09:16
  • Thank you so much, that fixed the problem, but now i get an error on the Model attribute – Pravania Reddy Sep 02 '21 at 09:23
  • This is the error code - CS1579. – Pravania Reddy Sep 02 '21 at 09:25
  • Would like to raise up a concern. Rather than directly pass the entity model to front-end, I would suggest that you use [Data Transfer Object (Dto)](https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5). Which Dto is used for the models for the display layer. This will be safer as you are not exposing the actual model/entity in DB. – Yong Shun Sep 02 '21 at 09:31
  • 1
    Thank you so much, I will have a look at this and give it a try now. Great! Thanks again, really appreciate it! – Pravania Reddy Sep 02 '21 at 09:33

2 Answers2

0

I think you must use thenInclude

 db.Users.Include(us => us.user).ThenInclude(us => us.designation).ToList()
Den
  • 650
  • 6
  • 15
0

create a list in your model as

public List<Designation> DesignationsList { get; set; }

you can use this list in your controller as

Designations.DesignationsList = _db.Designations.ToList();

then use the list in drop down as

 <div class="row form-group">
                <div class="col-md-2">
                    <label asp-for="DesignationID" class="control-label"></label>
                </div>
                <div class="col-md-10">
                    <select id="drpEmpList" class="form-control" asp-for="DesignationID" asp-items="@(new SelectList(Model.DesignationList, "DesignationID", "DesignationName"))">
                        <option value="">--Select--</option>
                    </select>
                    <input type="hidden" asp-for="DesignationID" />

                </div>
            </div>

NOTE: use the classname, connection and other class members as per your project