试图将我的模型转换为IEnumerable <model> </model>

时间:2013-09-30 22:37:18

标签: c# asp.net-mvc linq asp.net-mvc-4 ienumerable

在我的MedicalProductController我试图将名为“product”的MedicalProduct(模型类)转换为IEnumerable<MedicalProduct>,以便将其传递给GetMedicalProductViewModelList,这将返回IEnumerable<MedicalProductViewModel>(viewModel类)。

我试图将SQL调用保持在最低限度。根据其他stackoverflow成员的建议。

但“产品”未能在IEnumerable<MedicalProduct>

中变成MedicalProductController

MedicalProductController

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);            
    }

以防它有用,这里是映射类:

MedicalProductMapper

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;
    }
}

1 个答案:

答案 0 :(得分:9)

而不是

var productList = product as IEnumerable<MedicalProduct>;

尝试

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

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

List是实现IEnumerable

的类型之一