how to show referenced collection in one table in c# mvc from mongodb

my referenced class models

public class Cars
    {
        public Cars()
        {
            this.carphoto = new HashSet<carphotos>();
            
        }
        [ScaffoldColumn(false)]
        [BsonId]
        public ObjectId carid { get; set; }

        [DisplayName("Make")]
        public string CarMakeid { get; set; }

        [DisplayName("Model")]
        public string  CarModelid { get; set; }

        [DisplayName("Version")]
        public string CarVersionid { get; set; }

       
        [DisplayName("Make Year")]
        public DateTime makeyear { get; set; }
        public string Memsid { get; set; }
        [DisplayName("Kms Driven")]
        public string kmsDriven { get; set; }

        [DisplayName("Country")]
        public short countryid { get; set; }

        [Required]
        [DisplayName("State")]
        public short stateid { get; set; }

        [Required]
        [DisplayName("City")]
        public short cityid { get; set; }

        [DisplayName("Pin code")]
        public int pincode { get; set; }

        [DisplayName("Expected Price in Rs.")]
        public int expectedprice { get; set; }

        [DisplayName("Description")]
        public string sellersNote { get; set; }
        public DateTime Entrydate { get; set; }

        public virtual carFeatures feature { get; set; }
        public virtual CarCondition  carcondition { get; set; }
        public virtual WarrantiesModifications warrentiesmodificaton { get; set; }
        public virtual CarMake carmake { get; set; }
        public virtual CarModel carmodel { get; set; }
        public virtual CarVersion carversion { get; set; }
        public virtual citytable city { get; set; }
        public virtual states states { get; set; }
        public virtual Country country { get; set; }

        public virtual Mems mems { get; set; }

        public virtual ICollection<carphotos> carphoto { get; set; } 
        public Cars(sellCar sc)
        {
            CarMakeid = sc.CarMakeid;
            CarModelid = sc.CarModelid;
            CarVersionid = sc.CarVersionid;
            makeyear = sc.makeyear;
            Memsid = sc.Memsid;
            kmsDriven = sc.kmsDriven;
            countryid = sc.countryid;
            stateid = sc.stateid;
            cityid = sc.cityid;
            pincode = sc.pincode;
            expectedprice = sc.expectedprice;
            sellersNote = sc.sellersNote;
            Entrydate = sc.Entrydate; 
        }
    }
 public partial class CarModel
    {
        [BsonId]
        public ObjectId CarModelid { get; set; }
        [DisplayName("Make")]
        public string CarMakeid { get; set; }
        [DisplayName("Model")]
        public string model { get; set; }

        [DisplayName("Make")]
        [ScaffoldColumn(false)]
        public virtual CarMake carmake { get; set; }
    }

    public partial class CarVersion
    {
        [BsonId]
        public ObjectId CarVersionid { get; set; }
        public string version { get; set; }
        public string CarModelid { get; set; }

        public CarModel carmodel { get; set; }
    }

my controller to show list

  public ActionResult Index()
        {
            var carforsell = context.car.FindAll() ;
            return View(carforsell);
        }

and my view 

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.carmake.make)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.carmodel.model)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.carversion.version)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.makeyear)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.mems.fname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.kmsDriven)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.country.countryname )
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.states.statename )
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.city.cityname )
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.pincode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.expectedprice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.sellersNote)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Entrydate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

unable to show carmodel name car version name from another collection how to show them

pjpatil

unable to show carmodel name car version name from another collection how to show them

Do you know how to load?

ignatandrei

pjpatil

unable to show carmodel name car version name from another collection how to show them

Do you know how to load?

do you mean how to insert ..?

yes

Usually, "show " something does not mean "insert" , but "load"

However, if either insert or load is involved , then you should consult Mongo documentation, not MVC forums.

ignatandrei

Usually, "show " something does not mean "insert" , but "load"

However, if either insert or load is involved , then you should consult Mongo documentation, not MVC forums.

then give me example for my code how to load 

Documentation of MongoDB

http://docs.mongodb.org/ecosystem/tutorial/use-csharp-driver/

thanks for your replies

instead of using different collection i embedded it in to same user table

and add make and model names instead of using ids

but unable to get text from dropdowns

any suggestions

pjpatil

but unable to get text from dropdowns

Does this means that the dropdown is loaded with data? How did you register the dropdown?

ignatandrei

Does this means that the dropdown is loaded with data? How did you register the dropdown?

yes i m getting data in dropdown 

OK. And what is the declaration in Razor and in browser html of the dropdown?

Leave a Reply