【问题标题】:ASP.Net MVC multiple list return from ControllerASP.Net MVC 多个列表从控制器返回
【发布时间】:2013-02-01 04:37:44
【问题描述】:

我正在使用 mvc4 剃须刀,这是第一个型号代码

public class WebLicenses
{
    public string WebLicenseName { get; set; }
    public int CustomerWebLicensesCount { get; set; }
}

public class WebApplication
{
    public string WebApplicationName { get; set; }
    public int Count { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
    public DateTime? EndDate { get; set; }

    public string Never { get; set; }
}
public class griddetail
{
    public griddetail( List<WebLicenses> wl1,List<WebApplication> wa1 )
    { 
        wl =wl1;
        wa = wa1;
    }
    //public List<WebLicenses> wl2 { get; private set; }

    public List<WebLicenses> wl { get; set; }
    public List<WebApplication> wa { get; set; }

}

这是我的第二个模型代码,它使用第一个模型代码作为(这里存储过程返回 2 个表 - WebLicenses,WebApplication(在下面的代码中出现错误 --commented)

public IEnumerable GetOutlookGridDetail(string customerId = "")
{
        List<WebLicenses> weblicenses = new List<WebLicenses>();
        List<WebApplication> webapplication = new List<WebApplication>();            
        griddetail returnValue = new griddetail(weblicenses, webapplication);          

        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SLIMConnectionString"].ConnectionString);
        SqlCommand cmd = new SqlCommand("selectCustomerDetail", cn);
        cmd.CommandType = CommandType.StoredProcedure;

        SqlParameter param_CustomerID = new SqlParameter("@CustomerID", SqlDbType.VarChar, 10);
        param_CustomerID.Value = customerId;
        cmd.Parameters.Add(param_CustomerID);

        try
        {
            cn.Open();

            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                WebLicenses newRecord = new WebLicenses();
                newRecord.WebLicenseName = Convert.ToString(reader["WebLicenseName"]);
                newRecord.CustomerWebLicensesCount = Convert.ToInt32(reader["CustomerWebLicensesCount"]);
                //returnValue.Add(newRecord);
                weblicenses.Add(newRecord);                    
            }

            reader.NextResult();

            while (reader.Read())
            {
                WebApplication newRecord = new WebApplication();

                newRecord.WebApplicationName = Convert.ToString(reader["WebApplicationName"]);
                newRecord.Count = Convert.ToInt32(reader["Count"]);
                newRecord.Never = Convert.ToString(reader["Never"]);
                webapplication.Add(newRecord);
            }
            returnValue.wl.Add(weblicenses); //here comes error (Argument 1: cannot convert from 'System.Collections.Generic.List<SLIM.Models.WebLicenses>' to 'SLIM.Models.WebLicenses')
        }
        catch (Exception)
        {
            // returnValue = null;
        }
        finally
        {
            cmd.Dispose();

            if (cn.State == ConnectionState.Open)
            {
                cn.Close();
            }
        }

        return returnValue;
}

这是我的控制器代码。

    [HttpPost]
    public ActionResult GridViewPartialView(string recordId)
    {
        IEnumerable result1 = (new SlimHomePageData()).GetRecords(recordId, "");
        IEnumerable result2 = (new SlimHomePageData()).GetRecords();
        List<object> finalResult = new List<object>();
        finalResult.Add(result1);
        finalResult.Add(result2);
        return PartialView("OutlookGridDetail", finalResult);            
    }

我想返回一个包含 2 个列表的列表(但出现上述错误)。还建议,这是正确的方法,因为存储过程返回多个表,我们想通过模型在视图中设置值。

【问题讨论】:

    标签: c# asp.net-mvc list return


    【解决方案1】:

    实例 weblicenses 是 List 类型。看起来它应该是 WebLicenses 的单个实例。

    您可能希望将 newRecord 实例添加到 returnValue.wl 列表而不是列表实例。

    编辑: 您不需要向 wl 列表属性添加任何内容,因为您正在添加到 WebLicenses 列表中。 returnValue 上的 wl 属性是对 WebLicences 实例的引用,因此如果您添加到 WebLicences,那么 wl 将显示相同的添加。

    【讨论】:

      【解决方案2】:

      我忘了给答案,但昨天有人给了减号。是的,时间太长了。我做了什么(我再次解释一切以了解更多):

      第一步:定义2类为

      public class WebLicenses
          {
              public int WebLicenseID { get; set; }
              public string WebLicenseName { get; set; }
              public int CustomerWebLicensesCount { get; set; }
              public string CreatedBy { get; set; }
              public string UpdatedBy { get; set; }
          }
      
      public class WebApplication
      {
          public int appID { get; set; }
          public string WebApplicationName { get; set; }
          public int Count { get; set; }
      
          [DisplayFormat(DataFormatString = "{0:dd.MM.yyyy}")]
          public DateTime? EndDate { get; set; }
      
          public bool Never { get; set; }
          public string CreatedBy { get; set; }
          public string UpdatedBy { get; set; }
      }
      
      public class griddetail
      {
          public griddetail(List<WebLicenses> wl1, List<WebApplication> wa1)
          {
              weblicences = wl1;
              webapplication = wa1;
          }
      
          public List<WebLicenses> weblicences { get; set; }
          public List<WebApplication> webapplication { get; set; }        
      }
      

      第2步:将控制器更改为

      1. 将返回类型 IEnumerable 更改为 griddetail
      2. 最后在griddetail 类中声明并分配这个嵌套类

        public class SlimHomePageData
        {
             private SLIMDataContext dc = new SLIMDataContext(ConfigurationManager.ConnectionStrings["SLIMConnectionString"].ConnectionString);
        .
        .
        .
        
         public IEnumerable GetOutlookGridDetail(string customerId = "")
        {
            List<WebLicenses> weblicenses = new List<WebLicenses>();
            List<WebApplication> webapplication = new  List<WebApplication>();            
           //remove from here and add at the end of this method 
           //griddetail returnValue = new griddetail(weblicenses, webapplication);          
        
           SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["SLIMConnectionString"].ConnectionString);
           SqlCommand cmd = new SqlCommand("selectCustomerDetail", cn);
           cmd.CommandType = CommandType.StoredProcedure;
        
           SqlParameter param_CustomerID = new SqlParameter("@CustomerID", SqlDbType.VarChar, 10);
           param_CustomerID.Value = customerId;
           cmd.Parameters.Add(param_CustomerID);
        
           try
           {
            cn.Open();
        
            SqlDataReader reader = cmd.ExecuteReader();
        
               if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        WebLicenses newRecord = new WebLicenses();
                        newRecord.WebLicenseName = Convert.ToString(reader["WebLicenseName"]);
                        newRecord.CustomerWebLicensesCount = Convert.ToInt32(reader["CustomerWebLicensesCount"]);
                        weblicenses.Add(newRecord);
                    }
                }
        
                reader.NextResult();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        WebApplication newRecord = new WebApplication();
        
                        newRecord.WebApplicationName = Convert.ToString(reader["WebApplicationName"]);
                        newRecord.Count = Convert.ToInt32(reader["Count"]);
        
                        bool never = false;
        
                        if (reader["Never"] != DBNull.Value)
                        {
                            bool.TryParse(reader["Never"].ToString(), out never);
                        }
        
                        if (reader["EndDate"] != DBNull.Value)
                        {
                            newRecord.EndDate = Convert.ToDateTime(reader["EndDate"]);
                        }
        
                        newRecord.Never = never;
        
                        webapplication.Add(newRecord);
                    }
                }
        
        
            reader.Close();
        
           }
           catch (Exception)
           {
               // returnValue = null;
           }
           finally
           {
               cmd.Dispose();
        
               if (cn.State == ConnectionState.Open)
               {
                   cn.Close();
               }
           }
        
               griddetail returnValue = new griddetail(weblicenses, webapplication, webcustomapplication, maintenance, smartcv, histry, doc);
            return returnValue;
           }
          .
           .
          }//closing of my SlimHomePageData class
        

      **第3步:**在ActionMethod中更改为:发布此问题后,我更改了方法,但您可以在上述问题中使用相同的方法概念。稍后我需要另一个类在返回时添加result1

            public ActionResult GridViewPartialView()
      {
                return View();
      }
      
      
      [HttpPost]
              public ActionResult childGridViewPartialView(string recordId, string filtervalue, string flag)
              {
                  if (flag == "GridRowChanged")
                  {
                      IEnumerable result1;
                      griddetail result3;
                      if (recordId != null)
                      {
                          result1 = (new SlimHomePageData()).GetRecords(recordId, "");  //another result to be add later at the end of action method
                          result3 = (new SlimHomePageData()).GetOutlookGridDetail(recordId); //this was in my question
                      }
                      else
                      {
                          result1 = new List<SlimHomePageRecord>();   //another result to be add later at the end of action method
                          result3 = (new SlimHomePageData()).GetOutlookGridDetail(recordId); //this was in my question
                      }
      
                      List<object> finalResult = new List<object>();
                      finalResult.Add(result1);
                      finalResult.Add(result3);
                      return PartialView("OutlookGridDetail", finalResult);  
                  }
                  else
                  {
                      return PartialView("childGridViewPartialView", (new SlimHomePageData()).GetRecords(null, null));
                  }
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-17
        • 1970-01-01
        • 2017-01-17
        • 2012-07-18
        • 2012-02-28
        • 1970-01-01
        • 2023-02-11
        • 1970-01-01
        相关资源
        最近更新 更多