0

I'm start learning ASP.NET MVC 4 and stuck displaying Data From 2 model in 1 File Here's my Model

 public class mst_item
{
    [Key]
    [DisplayName("Item Code")]
    [Required]
    public string item_code{get;set;}

    [DisplayName("Item Name")]
    [Required]
    public string item_name{get;set;}        

    [DisplayName("Unit")]
    [Required]
    public mst_item_unit unit_id{ get; set; }

}


public class mst_item_unit
{
    [Key]
    public int unit_id { get; set; }

    [DisplayName("Unit")]
    public string unit_name { get; set; }


}

Then My Controller :

 public ActionResult Item()
 {
        var list_item = db.mst_item.Include("mst_item_unit").ToList();

        return View(list_item);
 }

Then How to display the unit_name based on the mst_item.unit_id in View using INNER JOIN or Include? something like :

@foreach (var item in Model)
{
      @Html.DisplayFor(modelItem => item.item_name)
      @Html.DisplayFor(modelItem => item.unit_name)
}

I get stuck here, But I success while displaying the mst_item data without joining mst_item_unit (just display the ID based on mst_item.unit_id) before.

BhushanK
  • 1,205
  • 6
  • 23
  • 39
denny saputra
  • 41
  • 1
  • 6

2 Answers2

0

You should be creating a ViewModel that will relate both your models, populate that ViewModel in your controller and use that in your View. The below link is very much your case. Multiple Models in a Single View (C# MVC3)

Community
  • 1
  • 1
0

You just have a couple of issues:

  1. The parameter to Include should be a navigation property on your entity, not the table name. In other words, change it to:

    var list_item = db.mst_item.Include("unit_id").ToList();
    
  2. You have to access the properties of this second entity through the navigation property. In other words, this is the view code you would need:

    @foreach (var item in Model)
    {
        @Html.DisplayFor(modelItem => item.item_name)
        @Html.DisplayFor(modelItem => item.unit_id.unit_name)
    }
    

That said, you've also got some stylistic problems here, which since you're new, I'll point out.

  1. Class and property names should be camel-cased, i.e. MstItem, rather than mst_item.
  2. Navigation properties should be named after the objects they connect to, i.e. unit_id should be something like MstItemUnit, or just Unit, if you prefer. That removes the need to specify a display name for it, as well.
  3. The use of the _id suffix on this navigation property is particularly troubling, because it implies that this property is an int or Guid - you know, something that could be used an id - whereas actually you're referencing a full-fledged object.
  4. While not as important, it makes little sense to repeat the class name or a portion thereof in the property names of that class. For example, unit_name should just be Name. Obviously, it's the name of the unit, because that's the class.

With those in place, your code becomes much more readable and "human". For example:

@Html.DisplayFor(m => item.Name)
@Html.DisplayFor(m => item.Unit.Name)
Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • Thanks for your suggestion I'ts work when I changing my code style, I try your first solution changing var list_item = db.mst_item.Include("unit_id").ToList(); and @Html.DisplayFor(modelItem => item.unit_id.unit_name) But it's still shows error message : column unit_id_unit_id is not found and no suggestion of unit_name while I typing item.unit_id. So I tried to do your 2nd solution to change the code style Then I overide the Entity name and the property name rather than Changing my Tables or Field name with camel case, and it's works! and it's shows the item.Unit.name property suggestion – denny saputra Feb 05 '15 at 03:20