WHat you'll do is create a view model with the 2 diff object:
class ViewModel
{
public Person Student {get;set;}
public Person Parent {get;set;}
}
Then in your view, you'll end up with inputs that have prefixes:
@Html.TextBoxFor(model=>model.Student.Name)
And then when this is posted it'll be named: "student.name", so your action method has to have the matching prefixes (via the param names):
ActionResult Update(Person student, Person parent)
{ ... }
So the param name "student" will match to the inputs that start with "student." and then the "student.name" input will map to the student.Name property (if that made sense).
So, in short... you can emit inputs with prefixes and then the cooresponding action mthos has to have all the names line up.
What I've ended up is relating the student and parent
and in the students create view I added @Html.EditeorFor(m=>m.Parent);
this seems to work
but I need to add ViewBag.JobId=SelectedItems(...); for parents Job dropdown in Students action method. This seems counter intuitive to me because the Student controller neads to know about drop down fields of Parent view. Is there a better aproach?
In addition @Html.HiddenFor(parent=>parent.Id) int the EditorTemplates/Parent.cshtml causes validation error. I decided to add condition and render the hidden control only if action=="Edit". Am I reintventin wheel? Or is this ok?
It really sounds like you need to refactor this to use a view model.
I agree 100% .
XalifaAspNet - Go look at BrockAllen's earlier post. For this situation a ViewModel is the correct course of action and will only make your life easier moving forward. It may seem strange now, but it will make your app more maintainable and less likely to
introduce bugs. In addition the view model approach will make it so that you can send any extra data needed to the view (such as dropdown lists) without cluttering your model or using the ViewBag.
XalifaAspNet
Member
5 Points
19 Posts
Model Binding prefix
Mar 31, 2012 11:52 AM|LINK
Hi
I want to edit 2 entities on the same page, Student and Parent (both derive from Person)
Is there a way to tell MVC to prefix controls for Parent ?
mameenkhn
Contributor
2026 Points
391 Posts
Re: Model Binding prefix
Mar 31, 2012 11:59 AM|LINK
if(obj is Student) { //do something related to Student } else if(obj is Parent) { //do something related to Parent }--------------------------------------------------
Muhammad Amin
محمد امين
XalifaAspNet
Member
5 Points
19 Posts
Re: Model Binding prefix
Mar 31, 2012 12:19 PM|LINK
Thanks bro
But where should this code go into?
XalifaAspNet
Member
5 Points
19 Posts
Re: Model Binding prefix
Mar 31, 2012 12:22 PM|LINK
I wonna be able to write Action method like
Create(Student stud, Parent parent){
//...
}
and in the view
@Html.EditorFor(student)
<!--some html-->
@Html.EditorFor(parent)
krokonoster
Contributor
4291 Points
1352 Posts
Re: Model Binding prefix
Mar 31, 2012 12:34 PM|LINK
Not sure if I understand you correctly, but sounds like it would be easier to have 2 strongly typed partials (one for student, one for parent).
Each partial will submit to it's own controller action method.
How I would go about however is create a viewmodel combining the two. So you will ahve something like SomeModel.Student and SomeModel.Parent.
mameenkhn
Contributor
2026 Points
391 Posts
Re: Model Binding prefix
Mar 31, 2012 12:34 PM|LINK
Use ViewModel approach with partial views
--------------------------------------------------
Muhammad Amin
محمد امين
BrockAllen
All-Star
28052 Points
4996 Posts
MVP
Re: Model Binding prefix
Mar 31, 2012 01:57 PM|LINK
WHat you'll do is create a view model with the 2 diff object:
class ViewModel
{
public Person Student {get;set;}
public Person Parent {get;set;}
}
Then in your view, you'll end up with inputs that have prefixes:
@Html.TextBoxFor(model=>model.Student.Name)
And then when this is posted it'll be named: "student.name", so your action method has to have the matching prefixes (via the param names):
ActionResult Update(Person student, Person parent)
{ ... }
So the param name "student" will match to the inputs that start with "student." and then the "student.name" input will map to the student.Name property (if that made sense).
So, in short... you can emit inputs with prefixes and then the cooresponding action mthos has to have all the names line up.
HTH
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
XalifaAspNet
Member
5 Points
19 Posts
Re: Model Binding prefix
Apr 02, 2012 08:37 AM|LINK
What I've ended up is relating the student and parent
and in the students create view I added @Html.EditeorFor(m=>m.Parent);
this seems to work
but I need to add ViewBag.JobId=SelectedItems(...); for parents Job dropdown in Students action method. This seems counter intuitive to me because the Student controller neads to know about drop down fields of Parent view. Is there a better aproach?
In addition @Html.HiddenFor(parent=>parent.Id) int the EditorTemplates/Parent.cshtml causes validation error. I decided to add condition and render the hidden control only if action=="Edit". Am I reintventin wheel? Or is this ok?
Parent.cshtml
@if (ViewContext.RouteData.Values["action"] == "Edit") { @Html.HiddenFor(model => model.Id) }BrockAllen
All-Star
28052 Points
4996 Posts
MVP
Re: Model Binding prefix
Apr 02, 2012 07:00 PM|LINK
It really sounds like you need to refactor this to use a view model.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
CodeHobo
All-Star
18669 Points
2648 Posts
Re: Model Binding prefix
Apr 02, 2012 07:40 PM|LINK
I agree 100% .
XalifaAspNet - Go look at BrockAllen's earlier post. For this situation a ViewModel is the correct course of action and will only make your life easier moving forward. It may seem strange now, but it will make your app more maintainable and less likely to introduce bugs. In addition the view model approach will make it so that you can send any extra data needed to the view (such as dropdown lists) without cluttering your model or using the ViewBag.
Blog | Twitter : @Hattan