Let's ignore the "AJAX-y" aspects for a moment (because that's a different issue) and just look at passing data between views and controllers. I would first recommend that you check out the NerdDinner
Tutorialwhich provides some good insights into how MVC works and how you use some of the features of MVC.
To address your specific question of how data is passed from View to Controller and back, there are a few ways to do this. However, the one that tends to make sense to most people is the idea of using strongly-typed views.
Let's say you have a model called Person. For now, don't worry about how we store Person data - we just have a Person class in the Models folder inside your MVC project.
public class Person { public string FirstName; public string LastName; public Person() { FirstName = "John"; LastName = "Doe"; } }
When we want to display data about Person in a View, we make a request to a specific controller. In this case (and for clarity) we'll call this controller the MainController. This will go in the Controllers folder and will be called MainController. Let's call the Action (an action is really just a specialized method) we want to get data from Index. Due to how ASP.NET MVC routing works, the path to our server will be:http://localhost/Main/Index. Notice the Controller (minus the "Controller" name), and the Action make up the path. (The first part is your server name, of course.)
Let's look at your controller - I'm going to keep it very simple for now:
publicclassMainController:Controller{publicActionResultIndex(){Person person =newPerson();returnView(person);}}
What we have going on inside the Index Action is that it is returning a View (which, by default, has the same name as the Action) and a model to correspond with that view. Now, we have to create our view.
The important part here is that you want to strongly-type the model that is being returned in the controller to your view. You do that with this line (which is first in your aspx file).
Notice the "Inherits" attribute and notice that your Person model makes up that attribute.
Now, just code the rest of your view as normal. Let's say we want to display the current Person name, and allow someone to change the name. The page would look like this (I'm not making this pretty):
This is the important part about getting data back and forth between Controllers and Views. What we are doing here is that we are taking your strongly-typed view (which is typed with the Person class) and using helper methods (like LabelFor and TextBoxFor) to tie the model together with its data and, ultimately, together with the actions contained in the controller (which we have to finish developing here in one moment).
So, you can now see the data. But, if a user changes the name and clicks submit - we want the page to display the new name. This means we need to add one more action to MainController - the one that receives the data.
[HttpPost]publicActionResultIndex(Person person){// Do whatever you want with the Person model. Update a database, or whatever.returnView(person);}
This action looks very similar to the other action we just developed. However, this one takes a person object (from the form that is being submitted) and it gives the controller an opportunity to do whatever needs to be done with that object. Once this is done, you can choose to redirect to a different page, redisplay the page (useful if there are errors), or do any number of other things.
Again, this is ALL covered (and much more) in the NerdDinner Tutorial. I highly recommend you read and follow through that.
As for the AJAX-y aspects you discussed, the premise is still the same (although there is a little bit of JavaScript/jQuery work that goes on in there). I won't go into it now, but the basics are also covered in the NerdDinner tutorial.
I hope this gets you started. I remember being a bit confused too when I first started working with web technologies, so I hope this helps you out!
it boils down to httppost, httpget in your action methods. going over the tutorials from the beginning is the only way to understand what you are doing other than guessing
Mahesh108
0 Points
5 Posts
MVC Passing value from view page to controller
Nov 05, 2012 02:49 PM|LINK
Hi, I have a treeview in my user control. I have pulled this user control to my view page.
Now i want to get the value each node in my controller page. Kindly suggest me how to do that.
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:08 PM|LINK
Specify as Routevalue parameter in the control which is calling the controller action method.
Mahesh108
0 Points
5 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:12 PM|LINK
Hi can you specify little more..
That will help me..
Xequence
Contributor
4313 Points
1528 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:19 PM|LINK
strongly type your object inside of an html.beginform
Credentials
CPrakash82
All-Star
18256 Points
2837 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:25 PM|LINK
Can you post some code, so we can suggest how and where you will requires change?
Mahesh108
0 Points
5 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:30 PM|LINK
<%
using (Html.BeginForm())<div>
{%><uc1:MenuControl ID="MnuCrtl" runat="server" value="" />
</div> <%} %>
In Controller
public ActionResult Report(FormCollection oformCollection) {}
Xequence
Contributor
4313 Points
1528 Posts
Re: MVC Passing value from view page to controller
Nov 05, 2012 03:35 PM|LINK
http://www.asp.net/mvc/tutorials/views/dynamic-v-strongly-typed-views
Credentials
Young Yang -...
All-Star
21349 Points
1818 Posts
Microsoft
Re: MVC Passing value from view page to controller
Nov 06, 2012 07:03 AM|LINK
I dont think this will work fine in MVC, if you want a treeview, you can look at this sample:
http://mvctreeview.codeplex.com/
Hope this helpful
Regards
Feedback to us
Develop and promote your apps in Windows Store
RameshRajend...
Star
7983 Points
2099 Posts
Re: MVC Passing value from view page to controller
Nov 06, 2012 07:06 AM|LINK
http://stackoverflow.com/questions/3507651/passing-values-between-view-and-controller-in-mvc-2
Let's ignore the "AJAX-y" aspects for a moment (because that's a different issue) and just look at passing data between views and controllers. I would first recommend that you check out the NerdDinner Tutorialwhich provides some good insights into how MVC works and how you use some of the features of MVC.
To address your specific question of how data is passed from View to Controller and back, there are a few ways to do this. However, the one that tends to make sense to most people is the idea of using strongly-typed views.
Let's say you have a model called Person. For now, don't worry about how we store Person data - we just have a Person class in the Models folder inside your MVC project.
When we want to display data about Person in a View, we make a request to a specific controller. In this case (and for clarity) we'll call this controller the MainController. This will go in the Controllers folder and will be called MainController. Let's call the Action (an action is really just a specialized method) we want to get data from Index. Due to how ASP.NET MVC routing works, the path to our server will be:http://localhost/Main/Index. Notice the Controller (minus the "Controller" name), and the Action make up the path. (The first part is your server name, of course.)
Let's look at your controller - I'm going to keep it very simple for now:
What we have going on inside the Index Action is that it is returning a View (which, by default, has the same name as the Action) and a model to correspond with that view. Now, we have to create our view.
The important part here is that you want to strongly-type the model that is being returned in the controller to your view. You do that with this line (which is first in your aspx file).
Notice the "Inherits" attribute and notice that your Person model makes up that attribute.
Now, just code the rest of your view as normal. Let's say we want to display the current Person name, and allow someone to change the name. The page would look like this (I'm not making this pretty):
This is the important part about getting data back and forth between Controllers and Views. What we are doing here is that we are taking your strongly-typed view (which is typed with the Person class) and using helper methods (like LabelFor and TextBoxFor) to tie the model together with its data and, ultimately, together with the actions contained in the controller (which we have to finish developing here in one moment).
So, you can now see the data. But, if a user changes the name and clicks submit - we want the page to display the new name. This means we need to add one more action to MainController - the one that receives the data.
This action looks very similar to the other action we just developed. However, this one takes a person object (from the form that is being submitted) and it gives the controller an opportunity to do whatever needs to be done with that object. Once this is done, you can choose to redirect to a different page, redisplay the page (useful if there are errors), or do any number of other things.
Again, this is ALL covered (and much more) in the NerdDinner Tutorial. I highly recommend you read and follow through that.
As for the AJAX-y aspects you discussed, the premise is still the same (although there is a little bit of JavaScript/jQuery work that goes on in there). I won't go into it now, but the basics are also covered in the NerdDinner tutorial.
I hope this gets you started. I remember being a bit confused too when I first started working with web technologies, so I hope this helps you out!
Xequence
Contributor
4313 Points
1528 Posts
Re: MVC Passing value from view page to controller
Nov 06, 2012 02:38 PM|LINK
it boils down to httppost, httpget in your action methods. going over the tutorials from the beginning is the only way to understand what you are doing other than guessing
Credentials