首页 > 解决方案 > 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).

标签: asp.net-mvc

解决方案


Did you rebuild all projects? If Business project did not build to dll, the classes didnt show in Model list.


推荐阅读