Hello All,
I am new to MVC architecture. I am facing an issue in loading value from database in mvc view page.
I using MS sql server as backend and stored procedure to call value into controller action.
This is my view code to trigger the data retreive function
ShowSearchOptions
@using (Html.BeginForm("GetStudentList", "Register"))
{
@Html.AntiForgeryToken()
And the Controller action :-
public ActionResult GetStudentList(StudentRegistration regObj)
{
DataTable table = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(ConfigurationManager.ConnectionStrings["SCHLERP"].ToString()))
{
using (SqlDataAdapter adapter = new SqlDataAdapter("GetStudentDetails", sqlCon))
{
adapter.SelectCommand.Parameters.Add("@FULLNAME", SqlDbType.NVarChar).Value = regObj.FULLNAME;
adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
adapter.Fill(table);
if (table.Rows.Count > 0)
{
return View(table);
}
}
}
return View();
}
And the View page to display the data :-
@model SchoolERP.Models.StudentRegistration
@{
ViewBag.Title = "GetStudentList";
Layout = "~/Views/Shared/_Layout.cshtml";
}
GetStudentList
StudentRegistration
@Html.DisplayNameFor(model => model.FULLNAME)
@Html.DisplayFor(model => model.FULLNAME)
@Html.DisplayNameFor(model => model.DOB)
@Html.DisplayFor(model => model.DOB)
@Html.DisplayNameFor(model => model.GENDER)
@Html.DisplayFor(model => model.GENDER)
@Html.DisplayNameFor(model => model.PARENTNAME)
@Html.DisplayFor(model => model.PARENTNAME)
@Html.DisplayNameFor(model => model.EMAIL)
@Html.DisplayFor(model => model.EMAIL)
@Html.DisplayNameFor(model => model.NATIONALITY)
@Html.DisplayFor(model => model.NATIONALITY)
@Html.DisplayNameFor(model => model.PHONENUMBER)
@Html.DisplayFor(model => model.PHONENUMBER)
@Html.DisplayNameFor(model => model.ADDRESS)
@Html.DisplayFor(model => model.ADDRESS)
@Html.DisplayNameFor(model => model.USERNAME)
@Html.DisplayFor(model => model.USERNAME)
@Html.DisplayNameFor(model => model.PASSWORD)
@Html.DisplayFor(model => model.PASSWORD)
@Html.ActionLink("Edit", "Edit", new { id = Model.ID }) |
@Html.ActionLink("Back to List", "Index")
AND an error is coming :-
Server Error in '/' Application.
The model item passed into the dictionary is of type 'System.Data.DataTable', but this dictionary requires a model item of type 'SchoolERP.Models.StudentRegistration'.
Sonu GuptaPosted Aug 23, 2019, 5:08 AM