我正在尝试列出数据库中显示的数据,我创建了一个类ViewingGoals并在类中实现其业务逻辑ViewingGoalsBusinessLayer代码如下所示
ViewingGoals.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLayer
{
public class ViewingGoals
{
public string Goal { get; set; }
}
}
ViewingGoalsBusinessLayer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using BusinessLayer;
namespace BusinessLayer
{
public class ViewingGoalsBusinessLayer
{
public void ViewData(ViewingGoals viewgoal)
{
try
{
string Connectionstring = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
List<ViewingGoals> viewgoals = new List<ViewingGoals>();
using (SqlConnection con = new SqlConnection(Connectionstring))
{
SqlCommand cmd = new SqlCommand("ViewGoal", con);
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//ViewingGoals viewgoal = new ViewingGoals();
viewgoal.Goal = rdr["Goal"].ToString();
viewgoals.Add(viewgoal);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}
我的控制器代码是:
ViewingGoalsController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BusinessLayer;
namespace AppraisalManagementSystemFinal.Controllers
{
public class ViewingGoalsController : Controller
{
//
// GET: /ViewingGoals/
public ActionResult ViewingGoals()
{
ViewingGoalsBusinessLayer viewinggoalsbusinessLayer = new ViewingGoalsBusinessLayer();
List<ViewingGoals> viewgoals = viewinggoalsbusinessLayer.viewgoal.ToList();
return View(viewgoals);
}
}
}
在那个控制器中我试图从ViewingGoalBusinessLayer访问该对象 列表viewgoals = viewinggoalsbusinessLayer.viewgoal.ToList();我收到错误,因为不包含viewgoal的定义。
请帮助我克服它。
答案 0 :(得分:0)
当您使用viewinggoalsbusinessLayer.viewgoal
时,您正试图从您的viewinggoalsbusinessLayer对象访问viewgoal属性。
问题是你还没有在那个对象中定义这样的属性!
这是代码的更新部分
public class ViewingGoalsBusinessLayer
{
public List<ViewingGoals> ViewGoals;
public void ViewData(ViewingGoals viewgoal)
{
try
{
string Connectionstring = ConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
Viewgoals = new List<ViewingGoals>();
// Rest of code is unchanged ...
}
catch (Exception ex)
{
throw ex;
}
}
}
接下来的问题是您需要初始化该列表,而且我不确定您的ViewData函数正在做什么...