I have a form which is split in 7 difference ways, Personal information, Employer, Previous Employment etc basically its an application form.
Each section within the form has its model so
Personal Information Section points at -> CandidateDetails
Employer points at -> CandidateEmployment
etc
Now my question is this how can i wrap all these models in to one for example MasterModel and use it on the form, allowing me to access other models within the MasterModel etc?
how can i wrap all these models in to one for example MasterModel and use it on the form, allowing me to access other models within the MasterModel etc?
Exactly how you said it. Create a MasterModel class and all others will be properties with get/set on MasterModel.
Correct me if im being stupid, ViewModel is it just another Model which inherits all other models? then you just create the one view for the model and bosh there you have it?
Harrison.Sco...
Member
321 Points
456 Posts
How to have more than one Model on a form
Jan 18, 2013 01:09 PM|LINK
Hi All,
I have a form which is split in 7 difference ways, Personal information, Employer, Previous Employment etc basically its an application form.
Each section within the form has its model so
Personal Information Section points at -> CandidateDetails
Employer points at -> CandidateEmployment
etc
Now my question is this how can i wrap all these models in to one for example MasterModel and use it on the form, allowing me to access other models within the MasterModel etc?
Thanks
ignatandrei
All-Star
134535 Points
21583 Posts
Moderator
MVP
Re: How to have more than one Model on a form
Jan 18, 2013 01:13 PM|LINK
Exactly how you said it. Create a MasterModel class and all others will be properties with get/set on MasterModel.
sp00k
Participant
1916 Points
435 Posts
Re: How to have more than one Model on a form
Jan 18, 2013 01:13 PM|LINK
make a viewmodel containing the models you need in the view
public class myViewModel { public Car car {get;set;} public Truck truck {get;set;} ...... }Harrison.Sco...
Member
321 Points
456 Posts
Re: How to have more than one Model on a form
Jan 18, 2013 01:19 PM|LINK
Hi thanks for the information,
Correct me if im being stupid, ViewModel is it just another Model which inherits all other models? then you just create the one view for the model and bosh there you have it?
ignatandrei
All-Star
134535 Points
21583 Posts
Moderator
MVP
Re: How to have more than one Model on a form
Jan 18, 2013 01:53 PM|LINK
No, it does not inherit. It aggregates the other models.
Harrison.Sco...
Member
321 Points
456 Posts
Re: How to have more than one Model on a form
Jan 18, 2013 01:57 PM|LINK
Sorry thats what i mean lol dunno why i put Inherits
ckll
Member
160 Points
149 Posts
Re: How to have more than one Model on a form
Jan 18, 2013 06:32 PM|LINK
Hi Harrison,
Check here
Gaspard
Contributor
2066 Points
416 Posts
Re: How to have more than one Model on a form
Jan 18, 2013 07:02 PM|LINK
A good tutorial as far as a View Model is concerned at this web page
http://blogs.msdn.com/b/simonince/archive/2010/01/26/view-models-in-asp-net-mvc.aspx
Regards
Harrison.Sco...
Member
321 Points
456 Posts
Re: How to have more than one Model on a form
Jan 23, 2013 01:28 PM|LINK
Hi Sorry to post on a post which has been going for a few days/weeks since any one touched it!
but im having a problem referencing controls located in other models etc
i keep getting "Object reference not set to an instance of an object."
This is my view where im using all my models.
<div class="blue-border-form-details"> <h1>Personal Information</h1> <fieldset> <legend><label>* Title:</label><%= Html.DropDownList("CandidateTitle", Model.CandidateDetails.TitleCollection())%></legend> <legend><label>* First name:</label><%= Html.TextBox("CandidateFirstName", Model.CandidateDetails.CandidateFirstName, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Surname:</label><%= Html.TextBox("CandidateSurname", Model.CandidateDetails.CandidateSurname, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Known As:</label><%= Html.TextBox("CandidateKnownAs", Model.CandidateDetails.CandidateKnowAs, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Home Number:</label><%= Html.TextBox("CandidateHomeNum", Model.CandidateDetails.CandidateHomeNum, new { maxlength = 11, size = "30", @class = "number", autocomplete = "off" })%></legend> <legend><label>* Mobile Number:</label><%= Html.TextBox("CandidateMobileNum", Model.CandidateDetails.CandidateMobileNum, new { maxlength = 11, size = "30", @class = "number", autocomplete = "off" })%></legend> <legend><label>* Email:</label><%= Html.TextBox("CandidateEmail", Model.CandidateDetails.CandidateSurname, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Driving License:</label><%= Html.RadioButton("DrivingLicense", Model.CandidateDetails.DrivingLicense, true)%>Yes</legend><legend> <label> </label><%= Html.RadioButton("DrivingLicense", Model.CandidateDetails.DrivingLicense, false)%>No</legend> <legend><label>* Address</label><%= Html.TextBox("Address1", Model.CandidateAddress.Address1, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Address2</label><%= Html.TextBox("Address2", Model.CandidateAddress.Address2, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Address3</label><%= Html.TextBox("Address3", Model.CandidateAddress.Address3, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label> Address4</label><%= Html.TextBox("Address4", Model.CandidateAddress.Address4, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Postcode</label><%= Html.TextBox("PostCode", Model.CandidateAddress.PostCode, new { maxlength = 7, size = "30", autocomplete = "off" })%></legend> <legend><label>* Town</label><%= Html.TextBox("Town", Model.CandidateAddress.Town, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* County</label><%= Html.TextBox("County", Model.CandidateAddress.County, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> <legend><label>* Driving License:</label><%= Html.RadioButton("WorkedForBPDBefore", Model.CandidateApplication.WorkedForBPDBefore, true)%>You have worked for BPD before</legend> <legend><label> </label><%= Html.RadioButton("WorkedForBPDBefore", Model.CandidateApplication.WorkedForBPDBefore, false)%>Haven`t worked for BPD Before</legend> <legend><label>* If "Yes" When and What position did you apply for?</label><%= Html.TextBox("WhenWorkedForBPDBefore", Model.CandidateApplication.WhenWorkedForBPDBefore, new { maxlength = 50, size = "30", autocomplete = "off" })%></legend> </fieldset> </div>At the top of the page i have this
this is my ViewModel
namespace BpdJobsModal { public class CandidateApplicationForm // CandidateApplicationForm is the main Model on the View - ApplicationForm within BPD.Jobs { #region "Properties" public CandidateAddress CandidateAddress { get; set; } // Candidate Address Details public CandidateCheckEmail CandidateCheckEmail { get; set; } // Candidates Email address checked against the DB public CandidateApplication CandidateApplication { get; set; } // Candidates Application Details public CandidateDetails CandidateDetails { get; set; } // Candidates Details firstname, surname etc public CandidateEducation CandidateEducation { get; set; } // Candidates Education public CandidateEmployment CandidateEmployment { get; set; } // Candidates Current employment public CandidateEmploymentGaps CandidateEmploymentGaps { get; set; } // Candidates Employment gaps public CandidatePreviousEmployment CandidatePreviousEmployment { get; set; } // Candidates Previous Employments history public CandidateVoluntaryWork CandidateVoluntaryWork { get; set; } // Candidates Voluntary work, if any. #endregion } }and this is my controller which returns the model "CandidateApplicationForm"
First im checking the email address which is passed in is valid if it is then return the view with the Model
public ActionResult application(string EmailAddress) { if(string.IsNullOrWhiteSpace(EmailAddress)) { ViewData["Message"] = "Email Address Invalid"; return View(); } else { CandidateCheckEmail objCheckEmail = new CandidateCheckEmail(); CandidateCheckEmailManager objCheckEmailManager = new CandidateCheckEmailManager(); objCheckEmail.RecordExist = objCheckEmailManager.Load(EmailAddress); if (objCheckEmail.RecordExist > 0) { CandidateApplicationForm objCandidateApplicationForm = new CandidateApplicationForm(); return View(objCandidateApplicationForm); } else { ViewData["Message"] = "Your email address doesnt exist in our Database."; return View(); } } }Any help would be highly appreciated.
goel.ankit
Contributor
2531 Points
513 Posts
Re: How to have more than one Model on a form
Jan 23, 2013 01:52 PM|LINK
where are you getting this error in controller?
Ankit
(Please select 'Mark as Answer' if my response has helped you.)