How to read the data from a dropdown in ASP.Net MVC 3

后端 未结 3 881
半阙折子戏
半阙折子戏 2021-01-14 23:30

Step 1:

I\'m using a dropdown to display company locations. The list comes from the Location table.

Step 2:

While the user is registering, the drop

3条回答
  •  北海茫月
    2021-01-14 23:58

    Here is some sample code that you can modify and use in your scenario. I don't know what your code looks like so I created my own.

    In your view:

    @model YourProject.ViewModels.YourViewModel
    

    Your locations dropdown:

    Location:
    
         @Html.DropDownListFor(
              x => x.LocationId,
              new SelectList(Model.Locations, "Id", "Name", Model.LocationId),
              "-- Select --"
         )
         @Html.ValidationMessageFor(x => x.LocationId)
    
    

    Your view model:

    public class YourViewModel
    {
         // Partial class
    
         public int LocationId { get; set; }
         public IEnumerable Locations { get; set; }
    }
    

    Your create action method:

    public ActionResult Create()
    {
         YourViewModel viewModel = new YourViewModel
         {
              // Get all the locations from the database
              Locations = locationService.FindAll().Where(x => x.IsActive)
         }
    
         // Return the view model to the view
         // Always use a view model for your data
         return View(viewModel);
    }
    
    [HttpPost]
    public ActionResult Create(YourViewModel viewModel)
    {
         if (!ModelState.IsValid)
         {
              viewModel.Locations = locationService.FindAll().Where(x => x.IsActive);
    
              return View(viewModel);
         }
    
         // If you browse the values of viewModel you will see that LocationId will have the
         // value (unique identifier of location) already set.  Now that you have this value
         // you can do with it whatever you like.
    }
    

    Your location class:

    public class Location
    {
         public int Id { get; set; }
         public string Name { get; set; }
         public bool IsActive { get; set; }
    }
    

    This is simple as can come. I hope this helps :)

    UPDATE:

    My service layer is there for any further business logic and then it calls my repository layer to get the data from the database. I use Entity Framework code first. I also use Autofac for my IoC container.

    Your service layer:

    public class LocationService : ILocationService
    {
         private readonly ILocationRepository locationRepository;
    
         public LocationService(ILocationRepository locationRepository)
         {
              this.locationRepository = locationRepository;
         }
    
         public IEnumerable FindAll()
         {
              return locationRepository.FindAll();
         }
    }
    

    And your repository:

    public class LocationRepository : ILocationRepository
    {
         YourDbContext db = new YourDbContext();
    
         public IEnumerable FindAll()
         {
              return db.Locations.OrderBy(x => x.Name);
         }
    }
    

    Your database context class:

    public class YourDbContext : DbContext
    {
         public DbSet Locations { get; set; }
    }
    

提交回复
热议问题