2

步骤1:

我正在使用下拉列表来显示公司位置。该列表来自位置表。

第2步:

当用户注册时,下拉列表将显示有问题的位置。

当用户选择印度时,该值(位置名称)应存储在 UserLogin 表中。

如何从 ASP.Net MVC 3 的下拉列表中读取值?

4

3 回答 3

4

为您的表单创建一个 ViewModel

public class CompanyViewModel
{
  public int CompanyId { set;get;}
  // Other properties of Company
  public int SelectedLocationId { set;get;}
  public IEnumerable<Location> Locations { set;get;}
}

假设你有一个像这样的 Location 类

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

在 Register ( HTTPGET) Action 方法中,返回一个 CompanyViewModel 对象,其中包含从数据库填充的 Locations 到 View

public ActionReuslt Register()
{
  CompanyViewModel model=new CompanyViewModel();
  model.Locations =myRepositary.GetAllLocations();
  return View(model);
} 

假设GetAllLocations从您的存储库返回 Location 对象列表。

并在强类型为 CompanyViewModel 的注册视图中

@model CompanyViewModel
@using(Html.BeginForm())
{

  @Html.DropDownListFor(x=>x.SelectedLocationId,
                     new SelectList(Model.Locations ,"Id",
                     "Name"),"Select Location")

  <input type="submit" value="Save" />
}

现在编写一个HTTPPost动作方法来处理表单发布(当用户提交表单时)

[HttpPost]
public ActionResult Register(CompanyViewModel model)
{
 if(ModelState.IsValid)
 {
  // You will have selected Location id available in model.SelectedLocationId property now
  //Save and redirect.
 }
 //Model Validation failed. so Let us reload the locations again
 //because HTTP is stateless and ASP.NET MVC is true to HTTP ! :)
 model.Locations =myRepositary.GetAllLocations();
 return View(model);
}
于 2012-05-11T11:33:20.203 回答
3

下面是一些示例代码,您可以在场景中修改和使用它们。我不知道您的代码是什么样的,所以我创建了自己的代码。

在您看来:

@model YourProject.ViewModels.YourViewModel

您的位置下拉菜单:

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

您的视图模型:

public class YourViewModel
{
     // Partial class

     public int LocationId { get; set; }
     public IEnumerable<Location> Locations { get; set; }
}

您的创建操作方法:

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.
}

您的位置类别:

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

这很简单。我希望这有帮助 :)

更新:

我的服务层用于任何进一步的业务逻辑,然后它调用我的存储库层以从数据库中获取数据。我首先使用实体​​框架代码。我还将 Autofac 用于我的 IoC 容器。

您的服务层:

public class LocationService : ILocationService
{
     private readonly ILocationRepository locationRepository;

     public LocationService(ILocationRepository locationRepository)
     {
          this.locationRepository = locationRepository;
     }

     public IEnumerable<Location> FindAll()
     {
          return locationRepository.FindAll();
     }
}

还有你的存储库:

public class LocationRepository : ILocationRepository
{
     YourDbContext db = new YourDbContext();

     public IEnumerable<Location> FindAll()
     {
          return db.Locations.OrderBy(x => x.Name);
     }
}

您的数据库上下文类:

public class YourDbContext : DbContext
{
     public DbSet<Location> Locations { get; set; }
}
于 2012-05-11T11:47:07.597 回答
1

如果您传递 formcollection 则取决于您如何获取表单的值,然后您可以通过此访问它的值

public ActionResult MyAction (FormCollection form)
    {
        string value = form["DropDownListName"];
    }

或者您可以通过

string value = Request.Form["DropDownListName"];
于 2012-05-11T11:29:23.840 回答