Some out there must no a solution to this! its driving me absolutly mad! MVC seems to be so flipping complicated all i want is a table where the user writes some information and i loop through it and save it to the DB such a simple task in asp.net but sadly
iv been told i have to do it MVC..... otherwise i would of chosen asp.net.
The view that is returned consists of a ViewModel( which has diffrent models in it) as this is for an application form so i have 1 model for address, 1 model for personal details, 1 model for employment etc 8 models in total
This is my controller action which returns the ViewModel and the model in question is CandidatePreviousEmployment
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)
{
var ViewModel = new CandidateApplicationForm
{
CandidateAddress = new CandidateAddress(),
CandidateApplication = new CandidateApplication(),
CandidateDetails = new CandidateDetails(),
CandidateEducation = new CandidateEducation(),
CandidateEmployment = new CandidateEmployment(),
CandidateEmploymentGaps = new CandidateEmploymentGaps(),
CandidatePreviousEmployment = new CandidatePreviousEmployment(),
CandidateVoluntaryWork = new CandidateVoluntaryWork()
};
return View(ViewModel);
}
else
{
ViewData["Message"] = "Your email address doesnt exist in our Database.";
return View();
}
}
}
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
}
}
ignatandrei yes i looked at the links you provided but i went through each example to gain some knowledge on MVC and also try and find a solution for my current issue.
First of all in your view you expecting the List<CandidatePreviousEmployment>, it looks like you passing inside CandidateApplicationForm
a single instance of CandidatePreviousEmployment when you should pass a list
Change your view model (CandidateApplicationForm), change the
public CandidatePreviousEmployment CandidatePreviousEmployment { get; set; }
to
public IEnumerable<CandidatePreviousEmployment>CandidatePreviousEmployment{get;set;}
Im passingCandidateApplicationForm into the view because im using all the properties located on other models
"CandidateApplicationForm" is the ViewModel where iv put all the other models etc, Iv made the modifications to the ViewModel which is not below,
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 IEnumerable<CandidatePreviousEmployment> CandidatePreviousEmployment { get; set; } // Candidates Previous Employments history
public CandidateVoluntaryWork CandidateVoluntaryWork { get; set; } // Candidates Voluntary work, if any.
#endregion
}
}
im unsure on how to do the controller part?
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)
{
var ViewModel = new CandidateApplicationForm
{
CandidateAddress = new CandidateAddress(),
CandidateApplication = new CandidateApplication(),
CandidateDetails = new CandidateDetails(),
CandidateEducation = new CandidateEducation(),
CandidateEmployment = new CandidateEmployment(),
CandidateEmploymentGaps = new CandidateEmploymentGaps(),
CandidatePreviousEmployment = new CandidatePreviousEmployment(),
CandidateVoluntaryWork = new CandidateVoluntaryWork()
};
return View(ViewModel);
}
else
{
ViewData["Message"] = "Your email address doesnt exist in our Database.";
return View();
}
}
}
var ViewModel = new CandidateApplicationForm
{
CandidateAddress = new CandidateAddress(),
CandidateApplication = new CandidateApplication(),
CandidateDetails = new CandidateDetails(),
CandidateEducation = new CandidateEducation(),
CandidateEmployment = new CandidateEmployment(),
CandidateEmploymentGaps = new CandidateEmploymentGaps(),
CandidatePreviousEmployment=new List<CandidatePreviousEmployment>(),
CandidateVoluntaryWork = new CandidateVoluntaryWork()
};
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
Hi thanks for your help on this really do appreciate it, was getting to the stage of pulling my hair out and throwing my PC out the window....which is the solution i know.
Iv just copied what you have and placed within my Controller so it looks like the below, but i have a red line under .Add it says
'System.Collections.Generic.IEnumerable<BPDJobsModal.CandidatePreviousEmployer>' does not contain a definition for 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable<BPDJobsModel.CandidatePreviousEmployment>' could be found
(are you missing a using directive of an assemble reference?)'
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)
{
var ViewModel = new CandidateApplicationForm
{
CandidateAddress = new CandidateAddress(),
CandidateApplication = new CandidateApplication(),
CandidateDetails = new CandidateDetails(),
CandidateEducation = new CandidateEducation(),
CandidateEmployment = new CandidateEmployment(),
CandidateEmploymentGaps = new CandidateEmploymentGaps(),
CandidatePreviousEmployment = new List<CandidatePreviousEmployment>(),
CandidateVoluntaryWork = new CandidateVoluntaryWork()
};
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());
return View(ViewModel);
}
else
{
ViewData["Message"] = "Your email address doesnt exist in our Database.";
return View();
}
}
}
This is my CandidatePreviousEmployment Model
namespace BpdJobsModal
{
public class CandidatePreviousEmployment
{
public List<JobDescription> JobDescriptionlist { get; set; }
#region " Properties "
[RegularExpression(".{2,}", ErrorMessage = "Invalid Employer")]
public string Employer { get; set; }
[RegularExpression(".{2,}", ErrorMessage = "Invalid Job Title")]
public string JobTitle { get; set; }
[RegularExpression(".{2,}", ErrorMessage = "Invalid From Date")]
public string FromDate { get; set; }
[RegularExpression(".{2,}", ErrorMessage = "Invalid To Date")]
public string ToDate { get; set; }
[RegularExpression(".{2,}", ErrorMessage = "Invalid Reason For Leaving")]
public string ReasonForLeaving { get; set; }
#endregion
}
}
Harrison.Sco...
Member
420 Points
530 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 12:49 PM|LINK
Some out there must no a solution to this! its driving me absolutly mad! MVC seems to be so flipping complicated all i want is a table where the user writes some information and i loop through it and save it to the DB such a simple task in asp.net but sadly iv been told i have to do it MVC..... otherwise i would of chosen asp.net.
Can someone please help me.
iluxa.v
Member
215 Points
47 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:02 PM|LINK
Hi,
Verify that:
1. @model IEnumerable<bpdjobsmodel.candidatepreviousemployment> - bpdjobsmodel.candidatepreviousemployment is a valid path to your model.
2. That you actually pass the populated model to the view via your action (return View(YourPopulatedModelList))
Can you show your controller action?
ignatandrei
All-Star
137645 Points
22141 Posts
Moderator
MVP
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:03 PM|LINK
Did you read the tutorials first? There are many examples here http://www.asp.net/mvc
Harrison.Sco...
Member
420 Points
530 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:16 PM|LINK
The view that is returned consists of a ViewModel( which has diffrent models in it) as this is for an application form so i have 1 model for address, 1 model for personal details, 1 model for employment etc 8 models in total
This is my controller action which returns the ViewModel and the model in question is CandidatePreviousEmployment
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) { var ViewModel = new CandidateApplicationForm { CandidateAddress = new CandidateAddress(), CandidateApplication = new CandidateApplication(), CandidateDetails = new CandidateDetails(), CandidateEducation = new CandidateEducation(), CandidateEmployment = new CandidateEmployment(), CandidateEmploymentGaps = new CandidateEmploymentGaps(), CandidatePreviousEmployment = new CandidatePreviousEmployment(), CandidateVoluntaryWork = new CandidateVoluntaryWork() }; return View(ViewModel); } else { ViewData["Message"] = "Your email address doesnt exist in our Database."; return View(); } } } This is my viewmodelnamespace 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 } }iluxa.v
Member
215 Points
47 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:29 PM|LINK
First of all in your view you expecting the List<CandidatePreviousEmployment>, it looks like you passing inside CandidateApplicationForm a single instance of CandidatePreviousEmployment when you should pass a list
Change your view model (CandidateApplicationForm), change the
public CandidatePreviousEmployment CandidatePreviousEmployment { get; set; }toand populate it in your controllerHarrison.Sco...
Member
420 Points
530 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:34 PM|LINK
Hi
Im passing CandidateApplicationForm into the view because im using all the properties located on other models "CandidateApplicationForm" is the ViewModel where iv put all the other models etc, Iv made the modifications to the ViewModel which is not below,
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 IEnumerable<CandidatePreviousEmployment> CandidatePreviousEmployment { get; set; } // Candidates Previous Employments history public CandidateVoluntaryWork CandidateVoluntaryWork { get; set; } // Candidates Voluntary work, if any. #endregion } }im unsure on how to do the controller part?
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) { var ViewModel = new CandidateApplicationForm { CandidateAddress = new CandidateAddress(), CandidateApplication = new CandidateApplication(), CandidateDetails = new CandidateDetails(), CandidateEducation = new CandidateEducation(), CandidateEmployment = new CandidateEmployment(), CandidateEmploymentGaps = new CandidateEmploymentGaps(), CandidatePreviousEmployment = new CandidatePreviousEmployment(), CandidateVoluntaryWork = new CandidateVoluntaryWork() }; return View(ViewModel); } else { ViewData["Message"] = "Your email address doesnt exist in our Database."; return View(); } } }iluxa.v
Member
215 Points
47 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:42 PM|LINK
After you change your model property from
try this:
var ViewModel = new CandidateApplicationForm { CandidateAddress = new CandidateAddress(), CandidateApplication = new CandidateApplication(), CandidateDetails = new CandidateDetails(), CandidateEducation = new CandidateEducation(), CandidateEmployment = new CandidateEmployment(), CandidateEmploymentGaps = new CandidateEmploymentGaps(), CandidatePreviousEmployment=new List<CandidatePreviousEmployment>(), CandidateVoluntaryWork = new CandidateVoluntaryWork() }; ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment()); ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment()); ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment());Harrison.Sco...
Member
420 Points
530 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:48 PM|LINK
Hi thanks for your help on this really do appreciate it, was getting to the stage of pulling my hair out and throwing my PC out the window....which is the solution i know.
Iv just copied what you have and placed within my Controller so it looks like the below, but i have a red line under .Add it says
'System.Collections.Generic.IEnumerable<BPDJobsModal.CandidatePreviousEmployer>' does not contain a definition for 'Add' accepting a first argument of type 'System.Collections.Generic.IEnumerable<BPDJobsModel.CandidatePreviousEmployment>' could be found (are you missing a using directive of an assemble reference?)'
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) { var ViewModel = new CandidateApplicationForm { CandidateAddress = new CandidateAddress(), CandidateApplication = new CandidateApplication(), CandidateDetails = new CandidateDetails(), CandidateEducation = new CandidateEducation(), CandidateEmployment = new CandidateEmployment(), CandidateEmploymentGaps = new CandidateEmploymentGaps(), CandidatePreviousEmployment = new List<CandidatePreviousEmployment>(), CandidateVoluntaryWork = new CandidateVoluntaryWork() }; ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment()); ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment()); ViewModel.CandidatePreviousEmployment.Add(new CandidatePreviousEmployment()); return View(ViewModel); } else { ViewData["Message"] = "Your email address doesnt exist in our Database."; return View(); } } }This is my CandidatePreviousEmployment Model
namespace BpdJobsModal { public class CandidatePreviousEmployment { public List<JobDescription> JobDescriptionlist { get; set; } #region " Properties " [RegularExpression(".{2,}", ErrorMessage = "Invalid Employer")] public string Employer { get; set; } [RegularExpression(".{2,}", ErrorMessage = "Invalid Job Title")] public string JobTitle { get; set; } [RegularExpression(".{2,}", ErrorMessage = "Invalid From Date")] public string FromDate { get; set; } [RegularExpression(".{2,}", ErrorMessage = "Invalid To Date")] public string ToDate { get; set; } [RegularExpression(".{2,}", ErrorMessage = "Invalid Reason For Leaving")] public string ReasonForLeaving { get; set; } #endregion } }iluxa.v
Member
215 Points
47 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:51 PM|LINK
You should also change the CandidateApplicationForm model
change the
public CandidatePreviousEmployment CandidatePreviousEmployment {get; set; }
to be
public List<CandidatePreviousEmployment> CandidatePreviousEmployment {get; set; }
Harrison.Sco...
Member
420 Points
530 Posts
Re: Loop through table and get users input and save to DB
Jan 24, 2013 01:56 PM|LINK
Hi iv done that,
now my properties in my table and underlined in red? and my project wont build?
<table> <tr> <td>Employer</td><td>Job Title</td><td>From</td><td>To</td><td>Reason for Leaving</td> </tr> <tr> <%foreach (var Item in Model.CandidatePreviousEmployment) %> <%{ %> <td> <%= Html.TextBox("Employer", Model.CandidatePreviousEmployment.Employer, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("JobTitle", Model.CandidatePreviousEmployment.JobTitle, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("FromDate", Model.CandidatePreviousEmployment.FromDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ToDate", Model.CandidatePreviousEmployment.ToDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ReasonForLeaving", Model.CandidatePreviousEmployment.ReasonForLeaving, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> </tr> <tr> <td> <%= Html.TextBox("Employer", Model.CandidatePreviousEmployment.Employer, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("JobTitle", Model.CandidatePreviousEmployment.JobTitle, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("FromDate", Model.CandidatePreviousEmployment.FromDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ToDate", Model.CandidatePreviousEmployment.ToDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ReasonForLeaving", Model.CandidatePreviousEmployment.ReasonForLeaving, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> </tr> <tr> <td> <%= Html.TextBox("Employer", Model.CandidatePreviousEmployment.Employer, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("JobTitle", Model.CandidatePreviousEmployment.JobTitle, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("FromDate", Model.CandidatePreviousEmployment.FromDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ToDate", Model.CandidatePreviousEmployment.ToDate, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <td> <%= Html.TextBox("ReasonForLeaving", Model.CandidatePreviousEmployment.ReasonForLeaving, new { maxlength = 50, size = "15", autocomplete = "off", @class = "EmployerTextBox" })%> </td> <%} %> </tr> </table>Well the end parts i.e employer, jobtitle etc