1future
1future

Reputation: 255

How can i pass a Model as a dropdownlist in MVC

I'm new to MVC and i'm having issues on how i can pass a model to a view as a dropdownlist. I have two models like this...

 public class CarModel
    {
       public int Id {get set;}
       public string Manufacturer {get;set;}

    } 


 public class Fuel
    {
     public int Id {get;set;}
     public string FuelType {get;set;}
    }



public ActionResult()
{
   //some logic here
   return View("Home", carModel)
}

And here is the view ..


 @model myproject.Models.CarModel
        <div class="row">
            <div class="col-md-3">
                @Html.LabelFor(m => m.Manufacturer)
            </div>

            <div class="col-md-9">
                @Html.EditorFor(m => m.Manufacturer)
            </div>
        </div>

I need to add a fueltype dropdownlist listing the fueltypes from the database. But i'm unsure on how i can access the Fuel Model here ...

<div class="row">
        <div class="col-md-3">
            @Html.LabelFor(m => m.FuelType)
        </div>
        <div class="col-md-9">
            @Html.DropDownListFor(m => m.FuelType )
        </div>

    </div>

What will be the best way for me to pass the Fuel Model to view and list the results in a dropdownlist?

Thank you

Upvotes: 0

Views: 413

Answers (1)

Deepak
Deepak

Reputation: 1584

Simply use DropDownListFor for binding list with drop down.

See more details here

DropDownList in MVC 4 with Razor

Upvotes: 1

Related Questions