将异步方法绑定到DropDownList ASP.NET MVC 5中
加工到:将异步方法绑定到DropDownList ASP.NET MVC 5中,asp.net,entity-framework,asynchronous,asp.net-mvc-5,async-await,Asp.net,Entity Framework,Asynchronous,Asp.net Mvc 5,Async Await,加工到: [Required(ErrorMessage = "This field is required")] [Display(Name = "Hardware Type")] public string HardwareType { get; set; } public IEnumerable<SelectListItem> ListOfHardwares { get; set; } public interface IHardwareRepository { Task<
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
[必需(ErrorMessage=“此字段是必需的”)]
[显示(名称=“硬件类型”)]
公共字符串硬件类型{get;set;}
公共IEnumerable硬件列表{get;set;}
iHardware假设:
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
公共接口IHardwareRepository
{
任务>GetAllHardwareAsync();
}
硬件存储:
public async Task<List<Entities.Hardware>> GetAllHardwareAsync()
{
var getAllHardware = await _appContext.Hardwares.ToListAsync();
return getAllHardware;
}
public异步任务GetAllHardwareAsync()
{
var getAllHardware=await_appContext.hardware.toListSync();
返回getAllHardware;
}
查看:
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
@LabelFor(model=>model.HardwareType,htmlAttributes:new{@class=“controllabel col-md-2”})
@Html.DropDownListFor(model=>model.HardwareType,model.listofhardware,“--Select--”,new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.HardwareType,“,new{@class=“text danger”})
编辑:
public async Task<List<Entities.Hardware>> GetAllHardwareAsync()
{
var getAllHardware = await _appContext.Hardwares.ToListAsync();
return getAllHardware;
}
控制器:
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
公共类硬件控制器:控制器
{
私人只读i工作单元(unitof工作单元);;
公共硬件控制器(IUnitOfWork unitOfWork)
{
_unitOfWork=unitOfWork;
}
公共异步任务DisplayAll()
{
var displayAll=await_unitOfWork.HardwareRepository.GetAllHardwareAsync();
返回视图(displayAll);
}
}
EDIT2:
控制器:
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
公共异步任务添加()
> {
>var模型=新的ImageDto();
>var hardwares=await_unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
>model.ListOfHardwares=硬件。选择(h=>new SelectListItem
> {
>Text=h.名称,
>值=h.说明
> });
>
>返回视图();
}
查看:
[Required(ErrorMessage = "This field is required")]
[Display(Name = "Hardware Type")]
public string HardwareType { get; set; }
public IEnumerable<SelectListItem> ListOfHardwares { get; set; }
public interface IHardwareRepository
{
Task<Entities.Hardware>> GetAllHardwareAsync();
}
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.HardwareType, Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
</div>
public class HardwareController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public HardwareController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<ActionResult> DisplayAll()
{
var displayAll = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(displayAll);
}
}
> public async Task<ActionResult> Add()
> {
> var model = new ImageDto();
> var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
>
> model.ListOfHardwares = hardwares.Select(h => new SelectListItem
> {
> Text = h.Name,
> Value = h.Description
> });
>
> return View();
}
@model MachineDto
<div class="form-group">
@Html.LabelFor(model => model.HardwareType, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Model.ListOfHardwares, "--Select--", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.HardwareType, "", new { @class = "text-danger" })
</div>
@model MachineDto
@LabelFor(model=>model.HardwareType,htmlAttributes:new{@class=“controllabel col-md-2”})
@DropDownListFor(model=>model.model.listofhardware,“--Select--”,new{@class=“formcontrol”})
@Html.ValidationMessageFor(model=>model.HardwareType,“,new{@class=“text danger”})
问题是,现在我想将GetAllHardwareAsync传递给硬件列表,并通过DropDownList显示它。我真的不想用ViewBag来做。有什么想法吗?
提前谢谢 在某个地方,您只需要使用存储库的实例在模型上设置值。在何处执行此操作取决于您在何处拥有(或可以获得)存储库实例以及您希望在模型上设置值的位置 例如,在大多数情况下,存储库将位于控制器上,控制器将构建要发送到视图的模型。可能看起来像这样
private IHardwareRepository _repository;
public SomeController(IHardwareRepository repository)
{
// I'm *assuming* you get the repository from dependency injection
_repository = repository;
}
public async Task<ActionResult> SomeAction()
{
var model = new MachineDto();
model.ListOfHardwares = await _repository.GetAllHardwaresAsync();
return View(model);
}
编辑:进一步检查后,您的模型似乎需要一个IEnumerable
,而不仅仅是实际数据本身。我通常建议在视图中而不是在模型中进行这种投影,但这并不足以完全阻止这种做法。特别是如果这个“模型”只是这个视图的一个视图模型
在这种情况下,您只需要一个简单的投影。一旦有了结果列表,就可以将其投影到所需类型的列表中。大概是这样的:
public async Task<ActionResult> SomeAction()
{
var repository = new HardwareRepository();
var model = new MachineDto();
model.ListOfHardwares = await repository.GetAllHardwaresAsync();
return View(model);
}
public async Task<ActionResult> DisplayAll()
{
var model = new MachineDto();
model.ListOfHardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
return View(model);
}
public async Task<ActionResult> DisplayAll()
{
var model = new MachineDto();
var hardwares = await _unitOfWork.HardwareRepository.GetAllHardwareAsync();
model.ListOfHardwares = hardwares.Select(h => new SelectListItem
{
Text = h.SomeTextProperty,
Value = h.SomeIdentifierProperty
});
return View(model);
}
public异步任务DisplayAll()
{
var模型=新机器到();
var hardwares=await_unitOfWork.HardwareRepository.GetAllHardwareAsync();
model.ListOfHardwares=硬件。选择(h=>new SelectListItem
{
Text=h.SomeTextProperty,
值=h.SomeIdentifierProperty
});
返回视图(模型);
}
您对
SomeTextProperty
和SomeIdentifierProperty
使用什么取决于Hardware
上存在哪些属性,以及您希望如何使用这些属性来构建选择列表。如果我像您一样编写,则使用:model.listofhardware=\u unitOfWork.HardwareRepository.getAllHardwareSync()
VS表示无法将类型“System.Threading.Tasks.Task”隐式转换为“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少转换?@Xardas:您忘记了wait
关键字。使用等待
,结果是一个列表
。没有它,结果就是一个任务
。正如错误所述,它们是不同的类型。事实上,我忘记了wait,但它仍然不能解决问题。@Xardas:你能进一步描述这个问题吗?它以什么方式失败?机器具有从硬件列表中获取的现场硬件类型。硬件列表直接取自数据库,因为硬件可以单独放入数据库。这意味着我有单独的硬件和机器存储库。问题是,我想将返回值从HardwareRepository(顺便说一句,它包含在UnitOfWork中)传递到机器中的硬件列表。起初,我是这样通过ViewBag完成的:ViewBag.ListOfRpsw=new SelectList(_rpswRepository.GetAllRpsw(),“Name”,“Description”)
成功了,但自从我添加了async&await和UnitOfWork之后,它就停止了工作。