这是通过从控制器添加视图并选择dto作为模板来完成的
我的DTO
public class Company_DTO
{
public long ID_Company { get; set; }
public string ESTATE_Company { get; set; }
}
MyController
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。
感谢您的帮助,希望我足够清楚。
答案 0 :(得分:1)
这应该可以解决您的问题:
查看
<div class="form-group">
@Html.DropDownListFor(model => model.ID_Company, new SelectList(ViewBag.Accounts, "ID_Company", "ESTATE_Company"))
</div>
假设您的视图是强类型(@model Company_DTO
)。
希望这会有所帮助
答案 1 :(得分:1)
考虑以下示例:
FROM prod.f31122 t
LEFT OUTER JOIN prod.f4801 wo ON t.wtdoco = wo.wadoco
AND t.wtmmcu = wo.wammcu
JOIN prod.f30008 r ON t.wtmmcu = r.iwmcu
JOIN prod.f0101 ab ON t.wtan8 = ab.aban8
因此,ViewBag.ListOfCompanies应包含IEnumerable。每个SelectListItem具有Text和Value属性,您需要分别分配ESTATE_Company和ID_Company。像这样的东西:
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)