为MVC模型排序db信息

时间:2014-02-11 22:38:14

标签: c# mysql asp.net-mvc dapper

FindByCounty() 这个函数应该转到数据库并提取信息。将信息存储在各自的类中(它确实如此)......但那又是什么?

public EditCountyViewModel FindByCounty(string countyName)
        {
            var parameters = new DynamicParameters();
            parameters.Add("@CountyName", value: countyName);

            var query = @"SELECT counties.id
                            , counties.CountyName
                            , counties.Website
                            , counties.Address
                            , counties.City
                            , counties.State
                            , counties.PhonePrimary
                            , counties.PhoneAlt
                            , counties.RecordsOnline
                            , counties.BackToYear
                            , counties.Cost
                            , products.ProductName
                            , products.Description
                            , countyproduct.TurnTime_MinHours
                            , countyproduct.TurnTime_MaxHours
                            , countyproduct.Price
                        FROM
                            counties, countyproduct, products
                        WHERE
                            counties.CountyName = @CountyName AND countyproduct.countiesID = counties.ID AND countyproduct.productsID = products.ID;";

            EditCountyViewModel editCountyVM = new EditCountyViewModel();
        EditProductsViewModel editProductVM = new EditProductsViewModel();
        editProductVM.ProductList = this.db.Query<Product>(query, new { countyName }).ToList();
        editCountyVM.county = this.db.Query<County>(query, new { countyName }).FirstOrDefault();


        return editCountyVM;
        }

HOMEcontroller上的详细信息() 我想我应该只查询所有信息,然后在此函数中使用LINQ来分割出我需要的信息。

public ActionResult Details(string countyName)
    {
        var model = repository.FindByCounty(countyName);
        return View(model);
    }

正如我的代码所代表的那样,它不会运行,因为在FindByCounty()中它只返回一个所需的元素editCountyVM。现在我可以更改函数的输出并返回另一个editProductVM但是我没有第一组信息(editCountyVM)。

我认为问题是我要获取此信息的方法。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

听起来如果您想同时返回editCountyVMeditProductVM,您可以将方法的返回类型更改为Tuple<EditCountyViewModel,EditProductsViewModel>。然后按如下方式生成return语句:

return new Tuple<EditCountyViewModel,EditProductsViewModel>(editCountyVM, editProductVM);