编辑视图模型的某些特性在ASP.NET MVC [英] Editing some properties of View Model in ASP.NET MVC

查看:178
本文介绍了编辑视图模型的某些特性在ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的实体框架数据库首页的办法。比方说,我有一个名为产品模型类和类有一个 NumberOfViews 属性。在修改的我通过了​​产品的实例给的控制器页的。

I'm using Entity Framework Database First approach. Let's say I have a model class called Product and that class has a NumberOfViews property. In the Edit page I pass an instance of the product class to the controller.

问题是我不能添加 @ Html.EditorFor(型号=> model.NumberOfViews)在修改的页,因为它应该是 NumberOfViews 与该产品每次访问页面更新,和不可以由网站管理员。

The problem is I can't add @Html.EditorFor(model => model.NumberOfViews) in the Edit page, because it's supposed that NumberOfViews is updated with every visit to the product page, and NOT by the website Admin.

和我不能将其添加为 @ Html.HiddenFor(型号=> model.NumberOfViews),因为如果管理的检查的元件,他可以手动编辑它。

And I can't add it as @Html.HiddenFor(model => model.NumberOfViews), because if the Admin Inspected the element, he can edit it manually.

另外,如果我尝试以编程方式设置在服务器端的值(例如, Product.NumberOfViews = db.Products.Find(Product.Id).NumberOfViews; ),我收到以下错误:

Also If I try to programmatically set the value on the server-side (e.g., Product.NumberOfViews = db.Products.Find(Product.Id).NumberOfViews;), I get the following error:

具有相同键的对象已经存在于ObjectStateManager。该ObjectStateManager无法跟踪多个对象使用相同的密钥。

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

如果我不把它添加到任何视图或控制器,该值将是,从而覆盖任何previous值。

And if I don't add it to either the view or the controller, the value will be null, thus overriding any previous value.

那么,应该怎么办?

推荐答案

我注意到有很多人使用相同的型号为他们的实体框架,因为他们为自己的MVC控制器做。我一般不鼓励这种做法。在我看来,一个数据库模式是不一样的视图模型。

I have noticed a lot of people use the same model for their Entity Framework as they do for their MVC Controller. I generally discourage this practice. In my opinion, a database model is not the same as a view model.

有时一个视图需要比数据库模型提供的信息较少。对于在修改账号密码为例,认为不需要名字,姓氏或电子邮件地址,即使他们可能都驻留在同一个表。

Sometimes a view needs less information than what the database model is supplying. For example while modifying account password, view does not need first name, last name, or email address even though they may all reside in the same table.

有时候需要从多个数据库表的信息。例如,如果用户可以存储电话号码的无限数量的为他们的个人资料,然后用户信息将在用户表,然后联系方式与在接触表。但是修改用户配置文件时,他们可能需要添加/修改/删除自己的号码中的一个或更多,因此视图需要的所有号码与姓,名和电子邮件地址。

Sometimes it needs information from more than one database table. For example if a user can store unlimited number of telephone numbers for their profile, then user information will be in user table and then contact information with be in contact table. However when modifying user profile, they may want to add/edit/delete one or more of their numbers, so the view needs all of the numbers along with first name, last name and email address.

这是我会做什么你的情况:

This is what I would do in your case:

// This is your Entity Framework Model class
[Table("Product")]
public class Product 
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ProductId { get; set; }

    public string Name { get; set; }

    public int NumberOfPageViews { get; set; }
}

// This is the model you will use in your Edit action.
public class EditProductViewModel
{
    public int ProductId { get; set; }

    public string Name { get; set; }
}

public class ProductController : Controller
{
    IProductService service;

    //...

    [HttpGet]
    public ActionResult Edit(int productId)
    {
        var product = service.GetProduct(productId);
        var model = new EditProductViewModel() 
        {
            ProductId = product.ProductId,
            Name = product.Name
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Edit(EditProductViewModel model)
    {
        if (ModelState.IsValid)
        {
            var product = service.GetProduct(model.ProductId);
            product.Name = model.Name;
            service.Update(product);
        }

        // ... 
    }
}

这篇关于编辑视图模型的某些特性在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆