【问题标题】:Why is ModelValidation Exception is raised always though the model resembles the table in MVC尽管模型类似于 MVC 中的表,但为什么总是引发 ModelValidation 异常
【发布时间】:2017-06-08 01:35:33
【问题描述】:

我是 MVC 的新手,我完全被模型验证异常卡住了。

我想使用实体框架从数据库表中检索数据。我使用 Visual Studio 2012 和 Sqlexpress 作为数据库服务器。这是我到目前为止所做的

  • 创建了代表表格的 Mobile 模型类(“Mobiles”)。
  • 创建了继承 DbContext 的 MobileContext 类。
  • 创建了 MobileController 类,该类返回由 Mobile 列表组成的视图。
  • 设计了视图。
  • 在 web.config 文件中设置连接字符串。
  • 现在我总是在 MobileContorller 类中得到模型验证异常。即使我为表和模型提供了相同的列名。我无法弄清楚究竟是什么导致了这个异常。 Table Mobiles 是

    模型 int NotNull 键入 nchar(10) NotNull 成本 nchar(10) NotNull

    为了方便起见,我考虑了成本。 手机模型类

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Web;
    namespace Mvcpractice4.Models
    {
        [Table("Mobiles")]
        public class Mobile
        {   public int Model { get; set; }
            public string Type { get; set; }
            public String Cost { get; set; }
        }
    }
    

    MobilesContorller

        namespace Mvcpractice4.Controllers
    {
        public class MobilesController : Controller
        {       
    
            public ActionResult Details(int id)
            {
                MobilesContext pc = new MobilesContext();
    
                List<Mobile> plist = pc.productsproperty.ToList();
    
                return View(plist);
            }
    
        }
    }
    

    MobilesContext 类

    namespace Mvcpractice4.Models
    {
        public class MobilesContext:DbContext
        {
            public DbSet<Mobile> productsproperty { get; set; }
        }
    }
    

    详情查看

    @model IEnumerable< Mvcpractice4.Models.Mobile>
    @using Mvcpractice4.Models;
    
    @{
        ViewBag.Title = "Details";
    }
    
    <h2>Details</h2>
    <ul>@foreach(Mobile obj in @Model)
        {
        <li>
            @obj.Model
        </li>
        <li>
            @obj.Type
        </li>
        <li>
            @obj.Cost
        </li>
    }
    </ul>
    

    而Web.Cofig文件中的连接字符串为

    <connectionStrings>
        <add name="MobilesContext" connectionString="server=.\sqlexpress;database=Gadgets;integrated Security=true" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    

    【问题讨论】:

    • 您得到的确切错误是什么?
    • 代码中何时何地出现异常?
    • 只有当您尝试将无效实体保存到数据库时才会引发此类异常。但是,就此而言,您发布的任何代码都不会尝试保存任何实体,甚至不会处理发布数据。发布实际引发异常的代码。
    • 在 MobilesController 类中。我在 ToList() 处收到“用户代码未处理 ModelValidation 异常”;方法。

    标签: asp.net-mvc entity-framework model-view-controller


    【解决方案1】:

    根据对similar post 的回答,我猜实体框架需要一个键——数据库中表的主键。

    [Table("Mobiles")]
    public class Mobile
    {   
        [Key]
        public int Model { get; set; }
        public string Type { get; set; }
        public String Cost { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-01
      • 2013-07-13
      • 1970-01-01
      • 2021-09-09
      相关资源
      最近更新 更多