根据 CRUD 模型获取下拉列表中的选定项目
Get selected item in a dropdown list in view to CRUD model
这是通过从我的控制器添加一个视图并选择我的 dto 作为模板来完成的
我的 DTO
public class Company_DTO
{
public long ID_Company { get; set; }
public string ESTATE_Company { get; set; }
}
我的控制器
public ActionResult UpdateCompany()
{
ViewBag.ListOfCompanies = DependencyFactory.Resolve<ICompanyBusiness>().GetCompany(); // this return a List<int> and following what I read for viewbag this should be right.
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateCompany([Bind]Company_DTO company_DTO)
{
try
{
//code
}
catch
{
return View();
}
}
查看
<div class="form-group">
@Html.DropDownListFor(model => model.ID_Company , ViewBag.ListOfCompanies) // Here I get an error on my @Html that my dto does nothave a list.
</div>
我希望选择的项目是 ID_Company,但是当我只想要选择的项目时,这里似乎试图添加整个列表,我找不到任何可以解决我的问题的文档或问题.
我无法编辑 DTO。
感谢您的帮助,希望我说得足够清楚。
这应该可以解决您的问题:
查看
<div class="form-group">
@Html.DropDownListFor(model => model.ID_Company, new SelectList(ViewBag.Accounts, "ID_Company", "ESTATE_Company"))
</div>
假设您的视图是强类型的 (@model Company_DTO
)。
希望对您有所帮助
考虑以下示例:
public class HomeController : Controller
{
private List<SelectListItem> items = new List<SelectListItem>()
{
new SelectListItem() { Text = "Zero", Value = "0"},
new SelectListItem() { Text = "One", Value = "1"},
new SelectListItem() { Text = "Two", Value = "2"}
};
public ActionResult Index()
{
ViewBag.Items = items;
return View(new Boo() { Id = 1, Name = "Boo name"});
}
}
public class Boo
{
public int Id { get; set; }
public string Name { get; set; }
}
the view:
@model WebApi.Controllers.Boo
@Html.DropDownListFor(x=>x.Id, (IEnumerable<SelectListItem>) ViewBag.Items)
所以,ViewBag.ListOfCompanies 应该包含 IEnumerable。每个 SelectListItem 都有 Text 和 Value 属性 ,您需要分别分配 ESTATE_Company 和 ID_Company 。像这样:
var companiesList = //get companies list
ViewBag.ListOfCompanies = companiesList.Select(x => new SelectListItem() {Text = x.ESTATE_Company, Value = x.ID_Company.ToString()});
....
@Html.DropDownListFor(x=>x.ID_Company, ViewBag.Items as IEnumerable<SelectListItem>)
这是通过从我的控制器添加一个视图并选择我的 dto 作为模板来完成的
我的 DTO
public class Company_DTO
{
public long ID_Company { get; set; }
public string ESTATE_Company { get; set; }
}
我的控制器
public ActionResult UpdateCompany()
{
ViewBag.ListOfCompanies = DependencyFactory.Resolve<ICompanyBusiness>().GetCompany(); // this return a List<int> and following what I read for viewbag this should be right.
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateCompany([Bind]Company_DTO company_DTO)
{
try
{
//code
}
catch
{
return View();
}
}
查看
<div class="form-group">
@Html.DropDownListFor(model => model.ID_Company , ViewBag.ListOfCompanies) // Here I get an error on my @Html that my dto does nothave a list.
</div>
我希望选择的项目是 ID_Company,但是当我只想要选择的项目时,这里似乎试图添加整个列表,我找不到任何可以解决我的问题的文档或问题.
我无法编辑 DTO。
感谢您的帮助,希望我说得足够清楚。
这应该可以解决您的问题:
查看
<div class="form-group">
@Html.DropDownListFor(model => model.ID_Company, new SelectList(ViewBag.Accounts, "ID_Company", "ESTATE_Company"))
</div>
假设您的视图是强类型的 (@model Company_DTO
)。
希望对您有所帮助
考虑以下示例:
public class HomeController : Controller
{
private List<SelectListItem> items = new List<SelectListItem>()
{
new SelectListItem() { Text = "Zero", Value = "0"},
new SelectListItem() { Text = "One", Value = "1"},
new SelectListItem() { Text = "Two", Value = "2"}
};
public ActionResult Index()
{
ViewBag.Items = items;
return View(new Boo() { Id = 1, Name = "Boo name"});
}
}
public class Boo
{
public int Id { get; set; }
public string Name { get; set; }
}
the view:
@model WebApi.Controllers.Boo
@Html.DropDownListFor(x=>x.Id, (IEnumerable<SelectListItem>) ViewBag.Items)
所以,ViewBag.ListOfCompanies 应该包含 IEnumerable。每个 SelectListItem 都有 Text 和 Value 属性 ,您需要分别分配 ESTATE_Company 和 ID_Company 。像这样:
var companiesList = //get companies list
ViewBag.ListOfCompanies = companiesList.Select(x => new SelectListItem() {Text = x.ESTATE_Company, Value = x.ID_Company.ToString()});
....
@Html.DropDownListFor(x=>x.ID_Company, ViewBag.Items as IEnumerable<SelectListItem>)