代码之家  ›  专栏  ›  技术社区  ›  dcolumbus

ASP.NET MVC:带模型的连接控制器

  •  0
  • dcolumbus  · 技术社区  · 15 年前

    我仍在学习,但在stackoverflow commnuties的帮助下,我可以越来越近。

    我现在有一个视图“Index.aspx”:

    System.Web.Mvc.ViewPage<Data.Models.GetDealsModel>
    

    模型:

    public class GetDealsModel
        {
            // set up the model
            public string DealId { get; set; }
            public string StreetAddress { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string ZipCode { get; set; }
            public string Logo { get; set; }
            public string Website { get; set; }
    
            public string TotalRows { get; set; }
    
        }
    

    控制器:

    public ActionResult Index()
            {
                LinqToDealsDataContext db = new LinqToDealsDataContext();
                XElement xmlTree = XElement.Parse("<Request><ZipCode>92612</ZipCode></Request>");
    
                var deals = db.spSearchDeals(xmlTree);
    
                return View(deals);
            }
    

    有了这个配置,我现在得到这个错误:

    The model item passed into the dictionary is of type 'System.Data.Linq.SqlClient.SqlProvider+SingleResult`1[Data.Models.spSearchDealsResult]', but this dictionary requires a model item of type 'Data.Models.GetDealsModel'.
    

    我猜我的控制器和我的模型连接有问题。。。我不知道为什么。请帮我接上这最后一个电话。

    注意:我确实理解,最终我应该将控制器中的逻辑分离为一个存储库模式,但是现在,这就可以了。

    3 回复  |  直到 15 年前
        1
  •  0
  •   awrigley    15 年前

    将下列项目添加到学习曲线中:

    1. 存储库模式

    2. 扪心自问:为什么我需要一个服务层?

    3. 读史蒂文·桑德森的书。它教你用MVC思考。

    以上内容适用于您的问题,因为您的问题显然与控制器中的代码应该在您的模型中有关(即,数据访问代码应该在存储库类中)。也就是说,你不是在考虑MVC。

    您的模型应该包括必要的存储库类,例如DealRepository。

    您需要一个服务类来将存储库从数据库中挖掘的对象映射到模型类:这样转换问题就可以封装到服务层代码中。

    如果这样做,则可以在控制器中写入:

    public ActionResult Index()
            {
                return(DealService.GetByZipcode(92612));
            }
    

    其中DealService.GetByZipcode基本上只是将DealRepository.GetByZipcode(92612)映射到模型类并返回映射结果。

    DealRepository.GetByZipcode方法大致如下:

    public static DealEntity GetByZipcode(string zip)
    {
                LinqToDealsDataContext db = new LinqToDealsDataContext();
                XElement xmlTree = XElement.Parse("<Request><ZipCode>" + zip + "</ZipCode></Request>");
    
                var deals = db.spSearchDeals(xmlTree);
    
                return deals;
            }
    

    DealEntity类就是Linq为您的表提供的任何内容。

    这一切的原因是:

    这种结构的原因如下:

    所有的数据访问代码都在一个地方:DealRepository。你可以独立测试和调试它。

    映射代码都在一个地方:DealService。你可以独立测试和调试它。

    换言之,你需要恰当地分离你的担忧。

    您现有代码的问题正是您没有分离关注点。也就是说,你使用了一些MVC,把它放进了一个食品加工机,结果却遇到了很多问题,这些问题比你需要的要难得多。

    您的模型混合到控制器中,没有存储库,没有服务层。

    所以,先别急,花点时间读史蒂夫·桑德森的书。

    我也会尝试建模一个更简单的问题。即使在好天气,xml解析也会让我头疼。

    注:

    您可以认真改进命名约定。LinqTodersDataContext?你在开玩笑吧?

        2
  •  2
  •   amurra    15 年前

    您需要翻译此呼叫返回的数据:

    var deals = db.spSearchDeals(xmlTree);
    

    变成一个 GetDealsModel 键入。比如说:

    GetDealsModel dealsModel = new GetDealsModel()
    {
       DealId = deals.DealId,
       StreetAddress = deals.StreetAddress,
    ....
    };
    
    return View(dealsModel);
    

    原因是您的视图是强类型的 获取数据模型 ,但你的 deals 变量不是那种类型,当您将它传递给视图时,它会给您一个异常。

        3
  •  1
  •   The Smallest    15 年前

    您应该创建GetDealsModel类型的对象,但是DB查询返回Data.Models.spSearchDealsResult类型的对象。尝试以下方法:

    return new GetDealsModel
               {
                  DealId = deals.Id,
                  // other fields here
               }
    
    
    推荐文章