asp.net-mvc - Using Business Object As Model is not working?
问题描述
I have probelem, when I add a class liabray to my MVC project as a business object named BusinessLayer having Employee and EmployeeBusinessLayer classes. Clicking on the add view in the index section of my home controller, then I should have Employee (BusinessLayer) as my model class. But actually, Employee (BusinessLayer) does not appear there?
I wanted to use businessobject as a model in my MVC project. I created a ClassLiabrary name BusinessLayer with two classes named Employee and EmployeeBusinessLayer. EmployeeBusinessLayer class has a property which returns employee list. After then, I created an EmployeeBusinessLayer object in my index action of my HomeController. The object return list of employee to a view. Then I right-clicked in the index section to add view. The viewname textbox already has text "Index", and I selected template as list. But the Model Class text box does only have the RouteConfig (UsingBusinessObjectAsModel), and I want that to be Employee (BusinessLayer)?
Namespace UsingBusinessObjectAsModel.Controllers
{
public class HomeController: Controller
{
// GET: Home
public ActionResult Index()
{
EmployeeBusinessLayer employeeBusinessLayer = new
EmployeeBusinessLayer();
List<Employee> employees =
employeeBusinessLayer.Employees.ToList();
return View (employees);
}
}
}
Here is my BusinessLayer :
namespace BusinessLayer
{
public class EmployeeBusinessLayer
{
public IEnumerable<Employee> Employees
{
get
{
string connectionString =
ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
List<Employee> employees = new List<Employee>();
using (SqlConnection con = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("spGetAllEmployees", con);
con.Open();
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Employee employee = new Employee();
employee.Id = Convert.ToInt32(rdr["EmployeeId"]);
employee.Name = rdr["Name"].ToString();
employee.Gender = rdr["Gender"].ToString();
employee.City = rdr["City"].ToString();
employees.Add(employee);
}
}
return employees;
}
}
}
}
I expect that when I right-clicked in the index action to add view and viewname and templated are selected then model class should have option for Employee (BusinessLayer) but it only have RouteConfig (UsingBusinessObjectAsModel).
解决方案
Did you rebuild all projects? If Business project did not build to dll, the classes didnt show in Model list.
推荐阅读
- .net-core - dotnet.exe 如何为 Blazor Web 程序集文件提供服务
- android - Firebase 安装服务不可用。请稍后再试
- powerbi-embedded - Power BI Embedded deleteVisual 在布局文件中的使用
- r - 尝试将 S4 类对象保存()为 R 中的二进制文件
- ansible - Jenkinsfile 和 ansibleplaybook 使用多个库存文件
- java - 每次使用 JPA 持久化同一个对象时,都会在数据库中插入一个新行
- excel - 有没有办法在重置行号时过滤excel,使其从1、2、3等......到最后?
- c# - 我想创建一个 C# 脚本,为我的格斗游戏创建一个随机无限地图
- python - 提取pandas DataFrame中的特定列值比较另一个DataFrame
- sharepoint - SharePoint - 简单登录列表