My task is to show multiple models into a single view.I've created a viewmodel for my requirement but I'm not meeting my requirement.
please have a look into the below code and rectify me where m i going wrong.------// Model 1
public partial class StudentsDetail
{
public int StudentID { get; set; }
public int ParentID { get; set; }
public string StudentName { get; set; }
public string Gender { get; set; }
public string FatherName { get; set; }
public string MotherName { get; set; }
public Nullable<System.DateTime> DateOfBirth { get; set; }
public virtual ParentsDetail ParentsDetail { get; set; }
public virtual SchoolDetail SchoolDetail { get; set; }
}
//Model 2
public partial class ParentsDetail
{
public ParentsDetail()
{
this.StudentsDetails = new HashSet<StudentsDetail>();
}
public int ParentID { get; set; }
public string Occupation { get; set; }
public string Organization { get; set; }
public string AnnualIncome { get; set; }
public virtual ICollection<StudentsDetail> StudentsDetails { get; set; }
}
//ViewModel Which I have created
public class ParentsInformationViewModel
{
public List<StudentsDetail> StudentsDetails { get; set; }
public List<ParentsDetail> ParentsDetails { get; set; }
public ParentsInformationViewModel(List<StudentsDetail> _studentDetails, List<ParentsDetail> _parentsDetails) //Should i pass all the required parameters that i want to display in view ????
{
StudentsDetails = _studentDetails;
ParentsDetails = _parentsDetails;
}
//And finally this is my method defined in the StudentController (Have i defined it in a right place/way??)
public ActionResult StudentViewModel()
{
ViewBag.ParentsDetail = new ParentsDetail(); //ParentsDetail is my controller
List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails();
List<ParentsInformationViewModel> ParentInfoVMObj = new List<ParentsInformationViewModel>();
//foreach (var student in studentListObj)
//{
// ParentInfoVMObj.Add(new ParentsInformationViewModel(student.StudentID, student.ParentID));
//}
//ParentInfoVMObj.Add(ParentInfoVMObj); /// don't know how to call the required viewmodel
return View(ParentInfoVMObj);
}
You didn't show the code, so I'm going to assume you declared your Razor or ASPX View's model to be ParentsInformationViewModel.
There are several issues in your action method:
You are passing a List<ParentsInformationViewModel> to the view instead of just a ParentsInformationViewModel object.
You never populate ParentInfoVMObj with the studentListObj or anything else.
As best as I can tell, your action method should look like this:
public ActionResult StudentViewModel()
{
List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails();
List<ParentsDetails> parentListObj = new List<ParentsDetails>();
// get list of parents here
ParentsInformationViewModel ParentInfoVMObj = new ParentsInformationViewModel();
ParentInfoVMObj.StudentDetails = studentListObj;
ParentInfoVMObj.ParentDetails = parentListObj;
return View(ParentInfoVMObj);
}
Darrell Norton, MVP
Darrell Norton's Blog Please click "Mark as Answer" if this helped you.
Marked as answer by rabbil on Jan 24, 2013 11:46 AM
Thanks For your Help, I tried with the similar code that you have added above with few modification in adding the respective lists(whichever I require to combine) and got the result to display in the view.
But still facing similar prob while adding other multiple model.do I need to define DAL separately for every viewmodel or any other idea.
public partial class RoleRegistrationDetails
{
public int RoleID { get; set; }
public string UserID { get; set; }
public string FullName { get; set; }
public virtual RoleType RoleType { get; set; } }
//model2
public partial class RoleType { public RoleType() { this.RoleRegistrationDetails= new HashSet<RoleRegistrationDetails>(); } public int RoleID { get; set; } public string RoleAbbreviation { get; set; } public string RoleName { get; set; }
}
In one of the controller this is my action method,where i want to edit or view details.
public ActionResult Edit(string id)
{
RoleRegistrationDetails roleregistrationdetails = new RoleRegistrationDetails();
var result= db.RoleRegistrationDetails.Find(id);
ViewBag.RoleID = new SelectList(db.RoleTypes, "RoleID", "RoleAbbreviation", role_registration_details.RoleID);
return View(role_registration_details);
}
public ViewResult Details(string id)
{
RoleRegistrationDetails roleRregistrationDetails = db.RoleRegistrationDetails .Find(id);
return View( roleRregistrationDetails );
}
But whenever I want to Edit or View the details then its not showing the full data, imean its showing only 2 values in the respective fields and no values while I'm viewing the details.I've used the below ViewModel, where am i going wrong ?
///viewmodel
public class RoleRegistrationViewModel
{
public RoleRegistrationDetails RoleRegistrationDetails { get; private set; } //RoleRegistrationDetails is one of the model
public SelectList RoleType { get; private set; } //RoleType is the 2nd model with some entities
public RoleRegistrationViewModel(RoleRegistrationDetails roleRegistrationDetails) {
RoleRegistrationDetails = roleRegistrationDetails;
RoleType = new SelectList("Select",RoleRegistrationDetails.RoleType);
}
}
how should i use both model to edit or view the details. ??
rabbil
Member
6 Points
14 Posts
How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Details s...
Jan 24, 2013 06:50 AM|LINK
Hi All,
My task is to show multiple models into a single view.I've created a viewmodel for my requirement but I'm not meeting my requirement.
please have a look into the below code and rectify me where m i going wrong.------// Model 1 public partial class StudentsDetail { public int StudentID { get; set; } public int ParentID { get; set; } public string StudentName { get; set; } public string Gender { get; set; } public string FatherName { get; set; } public string MotherName { get; set; } public Nullable<System.DateTime> DateOfBirth { get; set; } public virtual ParentsDetail ParentsDetail { get; set; } public virtual SchoolDetail SchoolDetail { get; set; } } //Model 2 public partial class ParentsDetail { public ParentsDetail() { this.StudentsDetails = new HashSet<StudentsDetail>(); } public int ParentID { get; set; } public string Occupation { get; set; } public string Organization { get; set; } public string AnnualIncome { get; set; } public virtual ICollection<StudentsDetail> StudentsDetails { get; set; } } //ViewModel Which I have created public class ParentsInformationViewModel { public List<StudentsDetail> StudentsDetails { get; set; } public List<ParentsDetail> ParentsDetails { get; set; } public ParentsInformationViewModel(List<StudentsDetail> _studentDetails, List<ParentsDetail> _parentsDetails) //Should i pass all the required parameters that i want to display in view ???? { StudentsDetails = _studentDetails; ParentsDetails = _parentsDetails; } //And finally this is my method defined in the StudentController (Have i defined it in a right place/way??)public ActionResult StudentViewModel() { ViewBag.ParentsDetail = new ParentsDetail(); //ParentsDetail is my controller List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails(); List<ParentsInformationViewModel> ParentInfoVMObj = new List<ParentsInformationViewModel>(); //foreach (var student in studentListObj) //{ // ParentInfoVMObj.Add(new ParentsInformationViewModel(student.StudentID, student.ParentID)); //} //ParentInfoVMObj.Add(ParentInfoVMObj); /// don't know how to call the required viewmodel return View(ParentInfoVMObj); }
I know that the above method of a viewmodel is wrong but how to use it or where am i going wrong I can't get.
Please, correct me as i'm a starter in MVC3 ,
i've followed this post http://forums.asp.net/t/1809094.aspx/1?How+to+show+in+1+view+different+Models
Thanks In Advance!!
DarrellNorto...
All-Star
86555 Points
9624 Posts
Moderator
MVP
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 09:59 AM|LINK
You didn't show the code, so I'm going to assume you declared your Razor or ASPX View's model to be ParentsInformationViewModel.
There are several issues in your action method:
As best as I can tell, your action method should look like this:
public ActionResult StudentViewModel() { List<StudentsDetail> studentListObj = StudentsDetailsDAL.GetStudentDetails(); List<ParentsDetails> parentListObj = new List<ParentsDetails>(); // get list of parents here ParentsInformationViewModel ParentInfoVMObj = new ParentsInformationViewModel(); ParentInfoVMObj.StudentDetails = studentListObj; ParentInfoVMObj.ParentDetails = parentListObj; return View(ParentInfoVMObj); }Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
asp.netforum...
Member
604 Points
132 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 10:09 AM|LINK
Use one model in view and u call another model in previous model .
Nisha Kant M...
Member
106 Points
39 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 10:47 AM|LINK
Write a wrapper model also known as ViewModel. E.g
Public class Company
{
public IEnumerable<Employee> Employess {get;set;}
public IEnumerable<Department> Departments {get;set};
etc....
}
rabbil
Member
6 Points
14 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:14 AM|LINK
Thanks For your Help, I tried with the similar code that you have added above with few modification in adding the respective lists(whichever I require to combine) and got the result to display in the view.
But still facing similar prob while adding other multiple model.do I need to define DAL separately for every viewmodel or any other idea.
Thanks a lot ..!!
asp.netforum...
Member
604 Points
132 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:19 AM|LINK
hai
//model 1 class
public class model1
{
public model2Name model2 {get;set;}
}
//model 2 class with properties
public class model2Name
{
public int a {get;set;}
}
//call razor view in
@model1.model2.a
//or,call any controller
model1 objmodel1 =new model1();
objmodel1.model2.a=1;
rabbil
Member
6 Points
14 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:20 AM|LINK
I've got the solution for this viewmodel but for my different ViewModel I still can't find out the solution. can i share you that code ?
rabbil
Member
6 Points
14 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:24 AM|LINK
when should I use IEnumerable w.r.t ViewModel and how ?
asp.netforum...
Member
604 Points
132 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:28 AM|LINK
rabbil
Member
6 Points
14 Posts
Re: How Can I show multiple models in a single view using ViewModel in Razor MVC (with only Detai...
Jan 24, 2013 11:45 AM|LINK
Other doubt related to viewmodel.....
//model 1
public partial class RoleRegistrationDetails { public int RoleID { get; set; } public string UserID { get; set; } public string FullName { get; set; }public virtual RoleType RoleType { get; set; }}
//model2
public partial class RoleType
{
public RoleType()
{
this.RoleRegistrationDetails= new HashSet<RoleRegistrationDetails>();
}
public int RoleID { get; set; }
public string RoleAbbreviation { get; set; }
public string RoleName { get; set; }
}
In one of the controller this is my action method,where i want to edit or view details.
public ActionResult Edit(string id) { RoleRegistrationDetails roleregistrationdetails = new RoleRegistrationDetails(); var result= db.RoleRegistrationDetails.Find(id); ViewBag.RoleID = new SelectList(db.RoleTypes, "RoleID", "RoleAbbreviation", role_registration_details.RoleID); return View(role_registration_details); } public ViewResult Details(string id) { RoleRegistrationDetails roleRregistrationDetails = db.RoleRegistrationDetails .Find(id); return View( roleRregistrationDetails ); }But whenever I want to Edit or View the details then its not showing the full data, imean its showing only 2 values in the respective fields and no values while I'm viewing the details.I've used the below ViewModel, where am i going wrong ?
///viewmodel
public class RoleRegistrationViewModel { public RoleRegistrationDetails RoleRegistrationDetails { get; private set; } //RoleRegistrationDetails is one of the model public SelectList RoleType { get; private set; } //RoleType is the 2nd model with some entities public RoleRegistrationViewModel(RoleRegistrationDetails roleRegistrationDetails) { RoleRegistrationDetails = roleRegistrationDetails; RoleType = new SelectList("Select",RoleRegistrationDetails.RoleType); } }
how should i use both model to edit or view the details. ??