i am fairly new in ASP.Net MVC. in my project i am not using Entity Framework rather i am using ADO.net. here is a code for my view model design. please have a look.
Action
public class WebGrid_Sample1Controller : Controller
{
// GET: WebGrid
public ActionResult Show1(StudentVm oSVm)
{
StudentVm SVm = new StudentVm(); //.GetStudents(oSVm);
SVm.Students= SVm.GetStudents(oSVm);
return View(SVm);
}
}
My Model and View model code
public class StudentVm
{
public int page { get; set; }
public int RowCount { get; set; }
public int PageSize { get; set; }
public int CurrentPage { get; set; }
public string sort { get; set; }
public string sortdir { get; set; }
public IList<Student> Students { get; set; }
public StudentVm()
{
PageSize = 5;
sort = "ID";
sortdir = "ASC";
CurrentPage = 1;
}
public IList<Student> GetStudents(StudentVm oSVm)
{
int StartIndex = 0, EndIndex = 0;
if (oSVm.page == 0)
oSVm.page = 1;
StartIndex = ((oSVm.page * oSVm.PageSize) - oSVm.PageSize) + 1;
EndIndex = (oSVm.page * oSVm.PageSize);
CurrentPage = StartIndex;
if (string.IsNullOrEmpty(oSVm.sort))
oSVm.sort = "ID";
if (string.IsNullOrEmpty(oSVm.sortdir))
oSVm.sortdir = "ASC";
string connectionStringName = System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString;
IList<Student> _Student = new List<Student>();
string strSQL = "SELECT ID, FirstName,LastName,IsActive,StateName,CityName FROM vwListStudents WHERE ID >=" + StartIndex + " AND ID <=" + EndIndex;
strSQL += " ORDER BY " + oSVm.sort + " " + oSVm.sortdir;
strSQL += ";SELECT COUNT(*) AS Count FROM vwListStudents";
using (SqlConnection connection = new SqlConnection(connectionStringName))
{
SqlCommand command = new SqlCommand(
strSQL, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
_Student.Add(new Student()
{
ID = Convert.ToInt32(reader["ID"].ToString()),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
IsActive = Convert.ToBoolean(reader["IsActive"]),
StateName = reader["StateName"].ToString(),
CityName = reader["CityName"].ToString()
});
}
}
reader.NextResult();
if (reader.HasRows)
{
while (reader.Read())
{
RowCount = Convert.ToInt32(reader["Count"].ToString());
}
}
reader.Close();
}
//RowCount = _Student.Count;
return _Student;
}
}
Model
public class Student
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsActive { get; set; }
public string StateName { get; set; }
public string CityName { get; set; }
}
some one review my view model code and said View models should not contain implementation. View Models are container that pass data between client, controller, and View.
he gave a new partial design of vm code
public class StudentVm
{
public int page { get; set; }
public int RowCount { get; set; }
public int PageSize { get; set; }
public int CurrentPage { get; set; }
public string sort { get; set; }
public string sortdir { get; set; }
public IList<Student> Students { get; set; }
}
public class Student
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsActive { get; set; }
public string StateName { get; set; }
public string CityName { get; set; }
}
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Show1(StudentVm oSVm)
{
return View(oSVm);
}
so my question is where to put the db interaction routine ?
i am talking about this line
public IList<Student> GetStudents(StudentVm oSVm)
{
int StartIndex = 0, EndIndex = 0;
if (oSVm.page == 0)
oSVm.page = 1;
StartIndex = ((oSVm.page * oSVm.PageSize) - oSVm.PageSize) + 1;
EndIndex = (oSVm.page * oSVm.PageSize);
CurrentPage = StartIndex;
if (string.IsNullOrEmpty(oSVm.sort))
oSVm.sort = "ID";
if (string.IsNullOrEmpty(oSVm.sortdir))
oSVm.sortdir = "ASC";
string connectionStringName = System.Configuration.ConfigurationManager.ConnectionStrings["StudentDBContext"].ConnectionString;
IList<Student> _Student = new List<Student>();
string strSQL = "SELECT ID, FirstName,LastName,IsActive,StateName,CityName FROM vwListStudents WHERE ID >=" + StartIndex + " AND ID <=" + EndIndex;
strSQL += " ORDER BY " + oSVm.sort + " " + oSVm.sortdir;
strSQL += ";SELECT COUNT(*) AS Count FROM vwListStudents";
using (SqlConnection connection = new SqlConnection(connectionStringName))
{
SqlCommand command = new SqlCommand(
strSQL, connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
_Student.Add(new Student()
{
ID = Convert.ToInt32(reader["ID"].ToString()),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
IsActive = Convert.ToBoolean(reader["IsActive"]),
StateName = reader["StateName"].ToString(),
CityName = reader["CityName"].ToString()
});
}
}
reader.NextResult();
if (reader.HasRows)
{
while (reader.Read())
{
RowCount = Convert.ToInt32(reader["Count"].ToString());
}
}
reader.Close();
}
//RowCount = _Student.Count;
return _Student;
}
so my request is anyone can restructure my code with db interaction routine. how to design the whole code including viewmodel, model and db interaction routine. i am using ADO.Net. so far whatever sample article i read to restructure my code all project sample using EF. so please some one restructure my code with db interaction routine. thanks in advance.