MVC 3 / EF 4.2 - 针对 ViewModel 进行编辑,我是否针对 Model 或 ViewModel 进行保存?

发布于 2025-01-03 21:11:03 字数 1544 浏览 1 评论 0 原文

我的第一个 MVC3 EF 4.2 站点,我对某些事情感到困惑,目前在 ViewModels 上查询和保存时。如果我解释得不好,请纠正我,我不知道如何术语。 .edmx 自动创建了表类,但我读到,考虑到我需要连接表来完全显示/编辑我的产品,最好创建一个 ViewModel。下面的控制器代码是我连接表以输出要编辑的产品的位置,然后保存。我的问题 - 将产品保存到 DbContext 生成的 Product.cs 模型或我自己的 ProductViewModel.cs 的正确方法是什么?

是否有更简单的方法来查询产品并连接表,然后映射到视图模型参数,或者我是否继续在控制器中执行所有这些操作,如下所示?

我还想在每次有人查看/单击产品时保存/更新产品,因此我不确定是否创建一个单独的 ViewModel 来仅更新该参数,或者再次使用 Product 模型。

希望这是有道理的!如果需要的话我可以进一步解释。

private SiteForgeEntities db = new SiteForgeEntities();

public ActionResult Edit(int id)
    {
        var viewModel = (
            from a in db.Products
            join b in db.Sites
            on a.SiteId equals b.SiteId
            join c in db.Sections
            on a.SectionId equals c.SectionId
            join d in db.Affiliates
            on a.AffiliateId equals d.AffiliateId
            select new ProductViewModel()
            {
                ProductId = a.ProductId,
                Product = a.Product,
                Description = a.Description,
                Image = a.Image,
                Price = a.Price,
                Clicks = a.Clicks,
                Link = a.Link,        
                Site = b.Site,
                Section = c.Section,
                Affiliate = d.Affiliate
            }).Single(x => x.ProductId == id);      

        return View(viewModel);
    }

    [HttpPost]       
    public ActionResult Edit(Product product)
    {
     ...update database...do I pass in and save back to Product or my ProductViewModel
    }

My first MVC3 EF 4.2 site and I'm confused on some things, currently on ViewModels when querying and saving. Please correct me if I explain this poorly, i'm not sure how to term this. The .edmx automatically created the table classes but I read it was better to create a ViewModel, considering I need to join tables to display/edit my Product completely. The controller code below is where I join tables to output a Product to edit, and then save. My question - what is the right way to save the Product, to the Product.cs model generated by DbContext or my own ProductViewModel.cs?

Is there an easier method to query a product and join the tables and then map to the viewmodels parameters, or do I keep doing all this in the controller like below?

I also want to save/update the product each time someone views/clicks on the product, so I wasn't sure if I create a separate ViewModel for updating just that parameter or again, use the Product model.

Hope that makes sense! I can explain further if needed.

private SiteForgeEntities db = new SiteForgeEntities();

public ActionResult Edit(int id)
    {
        var viewModel = (
            from a in db.Products
            join b in db.Sites
            on a.SiteId equals b.SiteId
            join c in db.Sections
            on a.SectionId equals c.SectionId
            join d in db.Affiliates
            on a.AffiliateId equals d.AffiliateId
            select new ProductViewModel()
            {
                ProductId = a.ProductId,
                Product = a.Product,
                Description = a.Description,
                Image = a.Image,
                Price = a.Price,
                Clicks = a.Clicks,
                Link = a.Link,        
                Site = b.Site,
                Section = c.Section,
                Affiliate = d.Affiliate
            }).Single(x => x.ProductId == id);      

        return View(viewModel);
    }

    [HttpPost]       
    public ActionResult Edit(Product product)
    {
     ...update database...do I pass in and save back to Product or my ProductViewModel
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放飞的风筝 2025-01-10 21:11:03

您使用ViewModel将多个模型传递给视图,但是当您保存数据时,您需要将其保存到适当的模型。如果您要添加或修改产品,您将向产品添加项目(使用 DbContext)。如果您在两个模型之间定义了一对多关系(在您的 Product.cs 模型中,您可能有一个属性声明为:

public virtual ICollection<SomeOtherModel> SomeOtherData { get; set; }

您可以使用它来构建表,而不是在 ViewModel 中传递所有内容。这里有一个很好的教程有关使用 EF4 的 CRUD 操作,请查看这些简短教程,它们可以帮助您了解您的策略 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc

You use ViewModel to pass multiple models to the view, but when you save data, you need to save it to the appropriate model. If you are adding or modifying products, you will add items to products (using your DbContext). If you have one-to-many relationship defined between two models (in your Product.cs model you might have a property declared as:

public virtual ICollection<SomeOtherModel> SomeOtherData { get; set; }

you can use this to build a table instead of passing everything in a ViewModel. There is a nice tutorial here regarding the CRUD operations using EF4. Have a look at these short tutorials that can give you an idea about your strategy http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文