【问题标题】:ASP.NET MVC4 with EF 6 Model Update Issue带有 EF 6 模型更新问题的 ASP.NET MVC4
【发布时间】:2014-04-26 02:55:30
【问题描述】:

我有两个模型如下

public class Category
{
     [Key]
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
     public int ID { get; set; },
     [Required]
     public string category { get; set; }
     [Required]
     public string Desc { get; set; }
}
public class Product
{
     [Key]
     [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
     public int ID { get; set; },
     public int CatID { get; set; },
     [ForeignKey("CatID")]
     public virtual Category Category { get; set; },
     [Required]
     public string Desc { get; set; },
     public string DisplayName
     {
         get
         {
             return string.format("{0} - {1}",this.Category.category,this.Desc);
         }
     }
}

这是我的编辑操作

public ActionResult Edit(int id)
{
     ViewBag.PossibleCategories = categoryRepository.All;
     return View(productRepository.Find(id));
}
[HttpPost]
public ActionResult Edit(Product product)
{
     if (ModelState.IsValid)  //<== This becomes false saying category.desc is required
     {
          productRepository.InsertOrUpdate(product);
          productRepository.Save();          
          return RedirectToAction("Index");
     }
     else
     {
          ViewBag.PossibleCategories = categoryRepository.All;
          return View();
     }
}

我有一个产品的编辑视图,它将 ID 和 DisplayName 显示为只读。所有其他字段均可编辑。

编辑视图也有产品 -> 类别 -> 类别有一个只读文本字段

@Html.TextBoxFor(model => model.Category.category, new Dictionary<string, object>() { { "readonly", "true" } })

回邮发送此内容并尝试创建一个新类别。这不是必需的。类别链接将使用 product.CatID 结转。

如何显示这些类型的字段??

当编辑视图回发时,模型状态显示为无效,因为产品的类别的 desc 为空(产品 -> 类别 -> desc)。 如果我注释掉 Product 中的 DisplayName 属性,则不会发生此问题。

据我了解,这是因为 DiaplayName 属性指的是 Category 属性,而视图视图没有 category.desc 字段,因此在 POST 操作上重新创建模型时,不会填充 desc。将 category.desc 字段添加到视图是解决此问题的一种方法。 有没有其他方法可以解决这个问题?

注意:这不是我遇到此问题的唯一型号。有许多复杂的模型存在相同的问题,对我来说,将这些字段也包含在视图中会导致 (1) 视图非常混乱 (2) 往返的数据量会很高。

【问题讨论】:

  • 如果你想看一下,我更新了我的答案。
  • 感谢您的回答,不得不下线一天!
  • 正如我在下面评论的那样,我误解了这个问题。我做了更正。再次@MiniRagnarok 抱歉混淆了。

标签: c# asp.net-mvc entity-framework


【解决方案1】:

简单的解决方案

检查是否为空。不管怎样,你真的应该养成这个习惯。

 public string DisplayName
 {
     get
     {
         if(this.Category != null)
         {
             return string.format("{0} - {1}",this.Category.category,this.Desc);
         }
         else
         {
             return String.Empty;
         }
     }
 }

复杂的解决方案

另一种解决方案是创建 ViewModel,而不是直接在视图中使用数据库模型。这些是专门为您的视图设计的模型。作为一个简化的示例,让我们使用您的 Product 模型并创建一个 ViewModel。

  1. 为您的 ViewModel 创建一个文件夹
  2. 创建与您的控制器匹配的 ViewModel 文件
  3. 创建一个将在视图中使用的 ViewModel

假设您有一个 Store Controller。这就是您要创建的文件结构。

Models
    ViewModels
        StoreViewModels.cs

在 StoreViewModels 中,您将创建一个名为 ProductViewModel 的 ViewModel,您将使用 Product 中的信息填充它。

public class ProductViewModel
{
    public int ID { get; set; }
    public string Description { get; set; }
    public string DisplayName { get; set; }

    public ProductViewModel() { }
    public ProductViewModel(Product product)
    {
        this.ID = product.ID;
        this.Description = product.Description;
        this.DisplayName = product.DisplayName;
    }
}

在您的视图中,您引用的是 ProductViewModel 而不是 Product。然后,在接收端,您将 ViewModel 字段转换回您的模型。如果您有任何问题,请告诉我。

【讨论】:

  • 对不起,我想我可能已经找到了错误的确切原因(我可能在最初的问题中误导了你,抱歉)我的视图有一个 category.category @ 的文本字段987654324@ 这是强制创建不需要的类别实体。类别通过下拉列表保存,因此 catid 可用于模型。这保持了类别和产品之间的联系。 product.Category.category 字段是只读的。它在那里用于显示目的。我也改变了上述问题。
  • @InsI 很高兴你知道了。我仍然推荐第二种解决方案。过去,它让我的生活更轻松。
  • 我无法解决问题。但我找到了导致问题的原因。我需要在视图中显示该字段。关于您的复杂解决方案,我进行了一些搜索,发现 automapper.org 将数据从一个对象复制到另一个对象。但我正在寻找替代方案,因为需要进行大量更改才能集成它。而我的时间线并没有那么多。可以在以后的 sprint 中对此进行研究。
猜你喜欢
  • 1970-01-01
  • 2016-03-08
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-21
  • 1970-01-01
相关资源
最近更新 更多