Reputation: 31
I have 5 classes:
1) EmployeeMainTable.cs (Employeeid is primary key and employee id is foreign key for rest of classes)
2) EmployeeFamilyTable.cs
3) previousEmployeement.cs
4) employeeaddress.cs
5) employeeEducation.cs
Each class contains respective properties enter code here
Now what I want to do for each class I have created 5 views to enter data and each view contain submit button to go to next view
My problem is I want to save all models at once when I click finish button in last view.
Please understand problem once am using mvc 4
I have tried this with following results:
@model SecondStep
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
CS1963: An expression tree may not contain a dynamic operation
getting error in
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
Line 13: {
Line 14:
Line 15: @Html.HiddenFor(model=>ViewBag.FirstStepModel) this line giving error
Line 16: @Html.EditorFor(model => model.SecondStepProp)
Line 17:
Upvotes: 2
Views: 2461
Reputation: 334
You should prolly pass previous view models to the "next" view, so when the "finish" button is clicked, you can retrieve all models (in the controller) to create at the end. (do not forget to put your models in the post)
UPDATE1:
Why do you need GET and POST Methods on controller ?
USPDATE2: example
**CASE 1 : If you have multiple view models **
Controller
[HttpGet]
public ActionResult FirstStep()
{
return View(new FirstStepModel());
}
[HttpPost]
public ActionResult SecondStep(FirstStepModel firstStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
return View(new SecondStepModel());
}
[HttpPost]
public ActionResult ThridStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
return View(new ThirdStepModel());
}
[HttpPost]
public ActionResult FourthStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
ViewBag.ThirdStepModel = thirdStepModel;
return View(new FourthStepModel()));
}
[HttpPost]
public ActionResult FifthStep(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel, FourthStepModel fourthStepModel)
{
ViewBag.FirstStepModel = firstStepModel;
ViewBag.SecondStepModel = secondStepModel;
ViewBag.ThirdStepModel = thirdStepModel;
ViewBag.FourthStepModel = fourthStepModel;
return View(new FinalStepModel());
}
[HttpPost]
public ActionRestul Finish(FirstStepModel firstStepModel, SecondStepModel secondStepModel, ThirdStepModel thirdStepModel, FourthStepModel fourthStepModel, FifthStepModel fifthStepModel)
{
// DB Save ?
}
Views FirstStep:
@model FirstStep
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
SecondStep:
@model SecondStep
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
ThirdStep:
@model ThirdStep
@using(Html.BeginForm("FourthStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
FourthStep:
@model FourthStep
@using(Html.BeginForm("FifthStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.HiddenFor(model => ViewBag.ThirdStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
FifthStep:
@model FifthStep
@using(Html.BeginForm("Finish", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => ViewBag.FirstStepModel.Value)
@Html.HiddenFor(model => ViewBag.SecondStepModel.Value)
@Html.HiddenFor(model => ViewBag.ThirdStepModel.Value)
@Html.HiddenFor(model => ViewBag.FifthStepModel.Value)
@Html.EditorFor(model => model.Value)
<input type="submit" value="Next"/>
}
CASE 2 : Single view model Carefull : for this to work, you will need (depending of the complexity of your model) a ModelBinder for your model
[HttpGet]
public ActionResult FirstStep()
{
return View("FirstStep", new Model());
}
[HttpPost]
public ActionResult SecondStep(Model model)
{
return View("SecondStep", model);
}
[HttpPost]
public ActionResult ThridStep(Model model)
{
return View("ThirdStep", model);
}
[HttpPost]
public ActionResult Finish(Model model)
{
// DB Save ?
}
...
Views
FirstStep:
@model Model
@using(Html.BeginForm("SecondStep", "controllerName", FormMethod.Post))
{
@Html.EditorFor(model => model.Step1Value)
<input type="submit" value="Next"/>
}
SecondStep:
@model Model
@using(Html.BeginForm("ThirdStep", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => model.Step1Value)
@Html.EditorFor(model => model.Step2Value)
<input type="submit" value="Next"/>
}
ThirdStep:
@model Model
@using(Html.BeginForm("Finish", "controllerName", FormMethod.Post))
{
@Html.HiddenFor(model => model.Step1Value)
@Html.HiddenFor(model => model.Step2Value)
@Html.EditorFor(model => model.Step3Value)
<input type="submit" value="Next"/>
}
Upvotes: 1