I want to display data from two tables in a view with the help of a stored procedure. I'm getting stuck in retrieving records, can't write view model class in DbSet
as its wrong, have posted my code.
And also I want to pass data from the controller to view in strongly typed.
Please refer snap short for further understanding.
The count of data is correct but there is no data in it.
Model
public class tblStudents
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime? CreatedDate { get; set; }
}
public class tblStudentDetails
{
[Key]
public int StudentId { get; set; }
public string Address { get; set; }
public DateTime? CreatedDate { get; set; }
}
//This class contains columns from both the tables
public class AllDataa
{
public class Dataa
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
}
Viewmodel:
public class VMStudentDetails
{
public List<tblStudents> students { get; set; }
public tblStudentDetails StudentDetails { get; set; }
public IEnumerable<AllDataa> AllData { get; set; }
}
Stored procedure:
CREATE PROCEDURE usp_StudentDetail
AS
BEGIN
SELECT
S.Name, SD.Address
FROM
tblStudents S
INNER JOIN
tblStudentDetails SD ON S.Id = Sd.StudentId
END
Controller
public ActionResult Index()
{
var model = new MVCLearning.Models.VMStudentDetails();
//What to write in Sqlquery?
//model.AllData = db.Database.SqlQuery<>("usp_StudentDetail").ToList();
//This is not working the data is not coming in Alldata
model.AllData = db.Database.SqlQuery<AllDataa>("usp_StudentDetail").ToList();
return View(model);
}