如何创建从存储过程访问数据的Razor MVC

时间:2013-02-18 19:28:03

标签: asp.net-mvc stored-procedures razor

MVC新手,我试图获得一个有效的模型视图组件。唯一的限制是数据由C#类中的存储过程填充,并且PartialView使用Razor并存储在_Shared文件夹中,因此所有页面都可以引用它。

我认为可能有用的是ViewModel,BreadcrumbViewModel.cs:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace Web.Controllers.ViewModels
{
  public class BreadcrumbViewModel
  {
    public int Id { get; set; }
    public int ParentId { get; set; }
    public string Label { get; set; }
    public string URL { get; set; }
    public int SortOrder { get; set; }
    public int InverseDepth { get; set; }
  }

  public class BreadcrumbsViewModel
  {
    public List<BreadcrumbViewModel> getBreadcrumbModel(string thisURL)
    {
        //thisURL contains the path of the current page, so the stored proc can find its parents.
        List<BreadcrumbViewModel> listBVM = new List<BreadcrumbViewModel>();
        BreadcrumbViewModel model;
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDataContext"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.Connection = conn;
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                cmd.CommandText = "getBreadcrumb";
                cmd.Parameters.Add(new SqlParameter("@thisURL", SqlDbType.NVarChar));
                cmd.Parameters["@thisURL"].Value = thisURL;

                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        model = new BreadcrumbViewModel
                        {
                            Id = Convert.ToInt32(reader[0]),
                            ParentId = Convert.ToInt32(reader[1]),
                            Label = reader[2].ToString(),
                            URL = reader[3].ToString(),
                            SortOrder = Convert.ToInt32(reader[4]),
                            InverseDepth = Convert.ToInt32(reader[5])
                        };
                        listBVM.Add(model);
                    }
                }
            }
        }
        if (listBVM == null)
        {
            model = new BreadcrumbViewModel
            {
                Id = 1,
                ParentId = 0,
                Label = "Home",
                URL = "/",
                SortOrder = 1,
                InverseDepth = 1
            };
            listBVM.Add(model);
        }
        return listBVM;
    }
  }
}

如果此ViewModel正确,那么引用List&lt;&gt;的Controller和PartialView会是什么?看起来像?

1 个答案:

答案 0 :(得分:1)

不应使用Viewmodels直接访问数据。您的控制器应该获取视图模型的数据,甚至控制器也应该依赖于某些数据访问类。

Viewmodels通常由包含数据字段的属性组成。他们不应该有任何行为,他们不应该访问数据库。

我首先将数据访问隔离到另一个类。

public class BreadcrumbProvider
{
    public List<BreadcrumbViewModel> getBreadcrumbModel(string thisURL)
    {
        //your method
    }
}

控制器

public ActionResult ListBreadcrump(string url)//you can also define a model here
{
    var model = new BreadcrumbProvider().getBreadcrumbModel(url);
    return View(model);
}

然后您可以在视图中将列表用作模型。

@model System.Collections.Generic.List<BreadcrumbViewModel>