Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在ASP.NETMVC3中从下拉列表中读取数据_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 如何在ASP.NETMVC3中从下拉列表中读取数据

Asp.net mvc 如何在ASP.NETMVC3中从下拉列表中读取数据,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,步骤1: 我用下拉菜单显示公司的位置。该列表来自位置表 步骤2: 当用户注册时,下拉列表将显示有问题的位置 当用户选择India时,该值(位置名称)应存储在UserLogin表中 如何从ASP.Net MVC 3中的下拉列表中读取值?这取决于您如何获取表单的值如果您正在传递formcollection,那么您只需通过此命令访问表单的值即可 public ActionResult MyAction (FormCollection form) { string value =

步骤1:

我用下拉菜单显示公司的位置。该列表来自位置表

步骤2:

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

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


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

这取决于您如何获取表单的值如果您正在传递formcollection,那么您只需通过此命令访问表单的值即可

public ActionResult MyAction (FormCollection form)
    {
        string value = form["DropDownListName"];
    }
或者你可以通过

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

为表单创建ViewModel

public class CompanyViewModel
{
  public int CompanyId { set;get;}
  // Other properties of Company
  public int SelectedLocationId { set;get;}
  public IEnumerable<Location> Locations { set;get;}
}
在Register(
HTTPGET
)操作方法中,返回一个CompanyViewModel对象,其中包含从数据库填充到视图的位置

public ActionReuslt Register()
{
  CompanyViewModel model=new CompanyViewModel();
  model.Locations =myRepositary.GetAllLocations();
  return View(model);
} 
假设
GetAllLocations
从存储库返回位置对象列表

在强类型为CompanyViewModel的Register视图中

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

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

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

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

在你看来:

@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 Location
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
}
这很简单。我希望这有帮助:)

更新:

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

您的服务层:

public class LocationService : ILocationService
{
     private readonly ILocationRepository locationRepository;

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

     public IEnumerable<Location> FindAll()
     {
          return locationRepository.FindAll();
     }
}
公共类定位服务:ILocationService
{
专用只读ILocationRepository locationRepository;
公共位置服务(ILocationRepository位置存储库)
{
this.locationRepository=locationRepository;
}
公共IEnumerable FindAll()
{
返回locationRepository.FindAll();
}
}
以及您的存储库:

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

     public IEnumerable<Location> FindAll()
     {
          return db.Locations.OrderBy(x => x.Name);
     }
}
公共类LocationRepository:ILocationRepository
{
YourDbContext db=newyourdbcontext();
公共IEnumerable FindAll()
{
返回db.Locations.OrderBy(x=>x.Name);
}
}
您的数据库上下文类:

public class YourDbContext : DbContext
{
     public DbSet<Location> Locations { get; set; }
}
public类YourDbContext:DbContext
{
公共数据库集位置{get;set;}
}

Hi Sundal,我这样写,public ActionResult Index(){ViewBag.OLocation=new SelectList(dbcontext.Organization_Details,“OName”,“OLocation”);},我在Index.cshtml中使用@html.Dropdownlist(“OLocation”)调用这个OLocation,现在如果用户单击submit按钮,应选择该值,然后将其存储到DB。。。现在解释版本Hi dude它正在工作,感谢您的回复…请参阅下面的链接一次,并回复我如果您有任何ideatry Brendan Vogt解决方案,它将在Locations=locationService.FindAll()中工作。其中(x=>x.IsActive)检查我的更新答案。locationService是可以处理任何业务逻辑的服务层。这是您的存储库层。您好,我对MVC概念不熟悉,过去我在MS CRM工作,在MVC工作不到15天……我对“代码优先”方法一无所知,我知道“数据库优先”方法到现在为止…任何人如何看到这一次,它有关于我的问题的完整细节你能告诉我你的Repostaty和GetAllLocations方法吗?我首先使用数据库,但不是代码优先的方法,请注意它,Repostaty和其他人可能不在这里工作…请在这里看到一次…@P_A_1:存储库不是仅与代码一起使用。存储库的概念是在数据访问层上有一个抽象。您可以简单地用对服务层/业务层/数据访问的调用来替换它,以获取数据。这意味着=myRepositary.GetAllLocations();可以是返回所有位置列表的方法。
public class Location
{
     public int Id { get; set; }
     public string Name { get; set; }
     public bool IsActive { get; set; }
}
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; }
}