MVC Drop Down list with entity framework

前端 未结 4 1230
闹比i
闹比i 2020-12-20 18:42

I\'ve created an MVC project using entity framework code first. My model is simply a form that gathers information.

public class Application
{
    public          


        
相关标签:
4条回答
  • 2020-12-20 19:25

    Very simple Code step by step

    1) In Entity Framework Class

    var productsList = (from product in dbContext.Products
                         select new ProductDTO
                         {
                           ProductId = product.ProductId,
                           ProductName = product.ProductName,
                           }).ToList();
    

    2) In Controller

    ViewBag.productsList = new EntityFrameWorkClass().GetBusinessSubCategoriesListForDD();
    

    3) In View

    @Html.DropDownList("Product_ProductId", new SelectList(ViewBag.productsList, "ProductId", "ProductName"), new { @class = "form-control" })
    

    OR

    @Html.DropDownListFor(m=>m.Product_ProductId, new SelectList(ViewBag.productsList , "ProductId", "ProductName"), new { @class = "form-control" })
    
    0 讨论(0)
  • 2020-12-20 19:31

    I assume there is a States model that has a Id and a StateName property.

    Change to the list to ViewData["State"] to ensure easy binding on POST.

    Id is the value that will be sent in the POST data ie.. State = Id. The StateName is what will be displayed in the Select list. So for your model this is not correct as State is a string. So needs to be

    this.ViewData["State"] = new SelectList(db.States.ToList(), "StateName", "StateName");
    

    Then in your view

    @Html.DropDownList("State")
    
    0 讨论(0)
  • 2020-12-20 19:35

    Its quite simple. Add an IEnumerable<SelectListItem> property to your model(Here I suggest you make a ViewModel that can have the exact same properties as Application with the below code included as a property). Then you just need to build the list and send it to your view

    public IEnumerable<SelectListItem> States{ get; set; }
    

    I will assume you want to retrieve the State values from the db. Here is how you will do it:

    private IEnumerable<SelectListItem> GetAllStates()
    {
        IEnumerable<SelectListItem> list = from s in db.Applications
                                           select new SelectListItem
                                           {
                                               Selected = false,
                                               Text = s.State,
                                               Value = s.State
                                           };
        return list;
    }
    

    Or this...

    private IEnumerable<SelectListItem> GetAllStates()
    {
        IEnumerable<SelectListItem> list = db.Applications.Select(s => new SelectListItem
                                           {
                                               Selected = false,
                                               Text = s.State,
                                               Value = s.State
                                           });
    
        return list;
    }
    

    Then do something like this in your action:

    var app = new Application
    {
        States = GetAllStates()
    };
    
    return View(app);
    

    Then finally, use Razor on the view to display the Dropdown list like this

    @Html.DropDownListFor(m => m.State, Model.States, "--Select a State--")
    

    The 1st parameter is the property of the model to update, the 2nd is the list of data, and 3rd is the default message that will be displayed

    Hope this helps.

    0 讨论(0)
  • 2020-12-20 19:39

    Create a data layer that retrieves a list of what you want. Then use EF to get all the states.

    //assuming you have a table of states..
    var states = db.States();
    

    The states table should be a Unique list of states.

    var selectList = new List<SelectListItem>(); 
    foreach(var thing in states){
        //if you got everything, thus the ID field for the value...   
       selectList.Add(new SelectListItem {Text =thing.State, Selected = false, Value = thing.ID);
    }
    

    Make sure in your Viewmodel class that selectlist is a public property.....and set to what you did above. You also need to provied a string for the view selection post back.

    StatesSelectList = selectList;
    public IEnumberable<SelectListItem> StatesSelectList {get;set;}
    public string SelectedState {get;set;}
    

    In your view, do this:

    @Html.DropDownListFor(p=>Model.SelectedState, Model.StatesSelectList)
    
    0 讨论(0)
提交回复
热议问题