Mvc下载列表与存储库

时间:2018-03-21 10:23:50

标签: c# asp.net-mvc

我正在尝试从下拉列表中选择ID,以便我可以根据我的业务逻辑方法设置它。我正在使用存储库模式,我的代码如下。

然而,当我运行程序时,我收到一个错误插入语句与外键冲突。

  

数据库表

public class Item
{
    [Key]
    public int ItemId { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public string ItemArtUrl { get; set; }


    public int CategoryId { get; set; }
    [ForeignKey("CategoryId")]
    public virtual Category Categories { get; set; }

}
  

数据库表

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual IEnumerable<Item> Items { get; set; }

}
  

视图模型

public class ItemViewModel
{
    [Key]
    public int ItemId { get; set; }        
    public string Title { get; set; }
    public decimal Price { get; set; }
    public string ItemArtUrl { get; set; }


    public int CategoryId { get; set; }
    [ForeignKey("CategoryId")]
    public IEnumerable<SelectListItem> CategoriesSelectListItem { get; set;}
}
  

视图模型

public class CategoryViewModel
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
}
  

业务逻辑层

public class ItemBusiness : IItemBusiness
{
    public void Insert(ItemViewModel item)
    {
        var itemrepo = new ItemRepository();
        Item i = new Item();
        i.Title = item.Title;
        i.Price = item.Price;
        i.ItemArtUrl = item.ItemArtUrl;
        i.CategoryId = item.CategoryId;
        itemrepo.Insert(i);
    }

    public List<ItemViewModel> GetAllItems()
    {
        var itemrepo = new ItemRepository();

        var query = (from x in itemrepo.GetAll()
                     select new ItemViewModel()
                     {
                         Title = x.Title,
                         Price = x.Price,
                         ItemArtUrl = x.ItemArtUrl,
                         CategoryId = x.CategoryId
                     }).ToList();

        return query;
    }
}
  

控制器

        [HttpGet]
    public ActionResult Create()
    {
        var categoryBusiness = new CategoryBusiness();
        ViewBag.items = new SelectList(categoryBusiness.GetAllCategories(), "CategoryId", "Name");
        return View();
    }

    [HttpPost]
    public ActionResult Create(ItemViewModel item)
    {
        var itemBusiness = new ItemBusiness();

        itemBusiness.Insert(item);

        return View();
    }

    public ActionResult Index()
    {
        var itemBusiness = new ItemBusiness();

        return View(itemBusiness.GetAllItems());
    }
}

1 个答案:

答案 0 :(得分:0)

我假设您的创建视图设置为使用ItemViewModel,因为您在[HTTPPost]中返回它而不是使用ViewBag.Items = dropdownlist,将ItemViewModel.CategoriesSelectListItem设置为该列表。

你必须更改你的控制器才能发送一个空的&#39; ItemViewModel除了CategoriesSelectionListItem

    public ActionResult Create()
{
    ItemViewModel ivm = new ItemViewModel();
    var categoryBusiness = new CategoryBusiness();
    ivm.CategoriesSelectionListItem = new SelectList(categoryBusiness.GetAllCategories(), "CategoryId", "Name");
    return View(ivm);
}

然后在您的视图中设置这样的下拉列表,它将返回CategoryId作为模型的一部分

    @Html.DropDownList("CategoryId", Model.CategoriesSelectListItem, htmlAttributes: new { @class = "form-control" })