如何在ASP.Net MVC 3下拉读取数据(How to read the data from a

2019-06-24 08:46发布

步骤1:

我使用一个下拉显示公司的位置。 列表来自位置表。

第2步:

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

当用户选择了印度,那么这个值(位置名称)应在用户登陆表存储。

我怎样才能读取在ASP.Net MVC 3下拉的价值?

Answer 1:

这取决于如果传递的FormCollection那么简单,你可以通过这个访问它的价值,你如何让你的表格值

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

或者,您可以通过访问

string value = Request.Form["DropDownListName"];


Answer 2:

创建表单一个ViewModel

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

假设你有一个位置类这样

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

在寄存器( HTTPGET )动作方法,返回一个CompanyViewModel对象从数据库填充[查看位置

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

假设GetAllLocations返回从repositary定位对象的列表。

而在注册查看它是强类型到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 actionmethod处理表单后(当用户提交表单)

[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);
}


Answer 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; }
}


文章来源: How to read the data from a dropdown in ASP.Net MVC 3