尝试将我的模型转换为 IEnumerable

本文关键字:IEnumerable Model 转换 我的 模型 | 更新日期: 2023-09-27 18:33:26

在我的MedicalProductController中,我正在尝试将名为"product"的MedicalProduct(模型类)转换为IEnumerable<MedicalProduct>,以便它可以传递给GetMedicalProductViewModelList,这将返回一个IEnumerable<MedicalProductViewModel>(viewModel类)。

我正在尝试将SQL调用保持在最低限度。 根据其他堆栈溢出成员的建议。

但是"产品"在我的MedicalProductController课上没有变成IEnumerable<MedicalProduct>

医疗产品控制器

public class MedicalProductController : Controller
{
    private MvcMedicalStoreDb _db = new MvcMedicalStoreDb();
    // other CRUD code omitted for brevity.
    // =============
    // Edit HttpGet
    // =============
    public ActionResult Edit(int id = 0)
    {
        MedicalProduct product = _db.Products.Find(id);
        if (product == null)
        {
            return HttpNotFound();
        }
        var productList = product as IEnumerable<MedicalProduct>;
        var viewModelList = GetMedicalProductViewModelList(productList);
        return View(viewModelList);
    }
    // =============
    // Edit HttpPost
    // =============
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(MedicalProduct product)
    {
        if (ModelState.IsValid)
        {
            _db.Entry(product).State = EntityState.Modified;
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        var productList = product as IEnumerable<MedicalProduct>;
        var viewModel = GetMedicalProductViewModelList(productList);
        return View(viewModel);
    }
    // =====================
    // Mapper Class Helper
    // =====================
    public IEnumerable<MedicalProductViewModel> GetMedicalProductViewModelList(IEnumerable<MedicalProduct> productList)
    {
        var brandList = _db.Brands.ToArray();
        var mapper = new MedicalProductMapper();
        return mapper.MapMedicalProductViewModel(productList, brandList);            
    }

为了以防万一有帮助,这里是映射类:

医疗产品映射仪

public class MedicalProductMapper
{
    // =====================
    // Model to ViewModel
    // =====================
    public IEnumerable<MedicalProductViewModel> MapMedicalProductViewModel(IEnumerable<MedicalProduct> medicalProductList, IEnumerable<Brand> brandList)
    {
        var brandSelectListItem = brandList.Select(b => new SelectListItem()
                                                {
                                                    Text = b.Name,
                                                    Value = b.Name
                                                });
        var viewModelList = medicalProductList.Select(p => new MedicalProductViewModel() 
                                {
                                    BrandID = p.BrandID,
                                    BrandName = brandList.SingleOrDefault(b => b.ID == p.BrandID).Name,
                                    BrandSelectListItem = brandSelectListItem,
                                    ID = p.ID,
                                    Price = p.Price,
                                    Name = p.Name
                                });

        return viewModelList;
    }
    // =====================
    // ViewModel to Model
    // =====================
    public MedicalProduct MapMedicalProduct(MedicalProductViewModel VM)
    {
        var model = new MedicalProduct()
        {
            Name = VM.Name,
            Price = VM.Price,
            BrandID = VM.BrandID
        };
        return model;
    }
}

尝试将我的模型转换为 IEnumerable<Model>

而不是

var productList = product as IEnumerable<MedicalProduct>;

尝试

var productList = new List<MedicalProduct> { product };

如果它不是IEnumerable,则无法强制转换为IEnumerable<MedicalProduct>,我在此示例中所做的是首先创建一个类型为 MedicalProduct 的新列表,然后将产品添加到列表中。

列表是实现 IEnumerable 的类型之一