步驟1:如何從ASP.Net MVC中的下拉菜單中讀取數據3
我正在使用下拉菜單來顯示公司位置。該列表來自位置表。
步驟2:
當用戶註冊時,下拉列表將顯示有問題的地點。
當用戶選擇印度時,此值(位置名稱)應存儲在用戶登錄表中。
如何從ASP.Net MVC 3的下拉菜單中讀取值?
步驟1:如何從ASP.Net MVC中的下拉菜單中讀取數據3
我正在使用下拉菜單來顯示公司位置。該列表來自位置表。
步驟2:
當用戶註冊時,下拉列表將顯示有問題的地點。
當用戶選擇印度時,此值(位置名稱)應存儲在用戶登錄表中。
如何從ASP.Net MVC 3的下拉菜單中讀取值?
這取決於如果傳遞的FormCollection那麼簡單,你可以通過這個
public ActionResult MyAction (FormCollection form)
{
string value = form["DropDownListName"];
}
訪問它的值,或者您可以訪問它通過
string value = Request.Form["DropDownListName"];
嗨Sundal,我像這樣寫, –
public ActionResult Index() { ViewBag.OLocation = new SelectList(dbcontext.Organization_Details,「OName」,「OLocation」); } 和我打電話此OLocation在Index.cshtml,通過使用 @ html.Dropdownlist(「OLocation」) 現在如果用戶點擊提交按鈕,然後,該值應選擇再存儲到數據庫... 現在解釋版本 –
你是否收到空? –
創建你如何讓你的表單值ViewModel for your form
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);
}
你可以分享我你的Repositaty和GetAllLocations方法 –
我是通過使用數據庫第一,但不是與代碼的第一種方法,並請介意它和Repositary和其他人可能無法在這裏工作... –
請在這裏看一次。 ..http://forums.asp.net/p/1802976/4977669.aspx/1?Re + Read + The + Data + From + DropDown + in + ASP + Net + MVC3 –
下面是一些示例代碼,您可以修改和使用您的方案。我不知道你的代碼是什麼樣的,所以我創建了自己的代碼。
在你看來:
@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; }
}
這是簡單的可以來。我希望這有助於:)
UPDATE:
我的服務層是沒有任何進一步的業務邏輯,然後調用我的倉庫層從數據庫中獲取數據。我首先使用實體框架代碼。我也爲我的IoC容器使用Autofac。
您的服務層:
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; }
}
嗨什麼是Locations = locationService.FindAll()中的「locationService」?Where(x => x.IsActive) –
檢查我更新的答案。 locationService是您可以處理任何業務邏輯的服務層。這就是調用你的存儲庫層。 –
嗨,我是這個MVC概念的新手,在過去我從事MS CRM工作,並且我在MVC中的工作時間不到15天... –
嘗試布倫丹·沃格特的解決方案,它將工作 – learning