RBS
asked on
MVC - Best way to pass viewModel data from one controller (form) to another
I have two forms, PreRegister.html and Register.cshtml.
PreRegister has a RadioButton group and I am trying to pass the user's selection - along with the CourseId that they had chosen to the Register controller/Form.
PreRegister.cshtml contains the following code:
In my controller, I want to capture the user's selection plus the CourseId that had been passed into the PreRegister controller and then pass these values in an object to the full Register controller/form
This is what I have so far:
So I have 2 questions:
Any help in figuring this out greatly appreciated.
RBS
PreRegister has a RadioButton group and I am trying to pass the user's selection - along with the CourseId that they had chosen to the Register controller/Form.
PreRegister.cshtml contains the following code:
<div class="controls">
<label class="radio">
<input checked="checked" id="rbGrp" name="rbGrp" type="radio" value="1" /><label><b>Yes</b></label>
<hr/>
</label>
<label class="radio">
<input id="rbGrp" name="rbGrp" type="radio" value="2" /><label><b>No</b></label>
</label>
</div>
In my controller, I want to capture the user's selection plus the CourseId that had been passed into the PreRegister controller and then pass these values in an object to the full Register controller/form
This is what I have so far:
public ActionResult PreRegister(int id)
{
var viewModel = new RegistrationViewModel();
{
viewModel.Enrollment.CourseId = id;
}
return View(viewModel);
}
[HttpPost]
public ActionResult PreRegister(RegistrationViewModel viewModel)
{
var enrollment = new Enrollment();
enrollment.CourseId = viewModel.Enrollment.CourseId;
enrollment.IsHdco = viewModel.Enrollment.IsHdco
//how to get IsHdco value from user selection of rbGrp?
return RedirectToAction("Register", viewModel?? enrollment??);
//how to pass the enrollment object over to Register?
}
public ActionResult Register(int id)
{
CodeCamper.Model.Course course = _coursesRepository.GetById(id);
var viewModel = new PageContentViewModel();
{
viewModel.PageContent = course.Description;
viewModel.Course = course;
}
return View(viewModel);
}
So I have 2 questions:
How do I get the value of the user selection from the radiobox group and translate it into a true/false?
How to I put this value plus CourseId into an object - either viewModel or Enrollment or something else and then transfer that as a parameter to the Register Controller that I want to geo to next?
Any help in figuring this out greatly appreciated.
RBS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The advantage is that you get strong-typing in your model and you don't have to futz around with the FormCollection.
In the Model:
public class MyModel
{
public string Choice { get; set; }
}
In the View:
@Html.RadioButtonFor( model => model.Choice, "1" )
@Html.RadioButtonFor( model => model.Choice, "2" )
In the Controller:
[HttpPost]
public ActionResult Foo( MyModel viewModel )
{
if( viewModel.Choice == "2" )
{
}
else if( viewModel.Choice == "1" )
{
}
}
In the Model:
public class MyModel
{
public string Choice { get; set; }
}
In the View:
@Html.RadioButtonFor( model => model.Choice, "1" )
@Html.RadioButtonFor( model => model.Choice, "2" )
In the Controller:
[HttpPost]
public ActionResult Foo( MyModel viewModel )
{
if( viewModel.Choice == "2" )
{
}
else if( viewModel.Choice == "1" )
{
}
}
ASKER
Hi Craig:
A belated note to thank you for your additional explanation & code. Clear and just what I needed. Thanks,
RBS
A belated note to thank you for your additional explanation & code. Clear and just what I needed. Thanks,
RBS
ASKER
Is there an advantage of making a radiobutton part of my model? Can you point to any resources that would explain how to do it or perhaps a sample line of code?
Thanks again for your explanation!
RBS