Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MVC3
I am a newbee trying to develop a MVC3 application. I needed to put a user control on a webpage. I understand that user controls in MVC3 are now partial views. I have written the control as a partial view and added it to the web page with @Html.Partial("_TimePanel") and it seems to work perfectly.
 
The problem I have is on the client side. The main view needs to collect data from the partial view and combine it with its own data to send back to the controller.
 
How do I use jscript on the parent view to collect data from the partial view?
Posted 14-Oct-12 8:00am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Well, usercontrol and partial view are not common concepts. Both render to html and both are embedded in a page, but that's all. If you try to treat a partial view as a user control, you mislead yourself. And that's what you did! The view on client side is just html (and js), thus a view can not collect data from the partial views in it. The view concept exists only on server side. At the end there is only a page. I am pretty sure your logical design has some flaws.
Actually you can do what you want on client side, but it makes few sense.
 
Let's suppose your two views are rendered to forms, and you want to get data from the input fields rendered by two different forms, and send it to one single action. Option one: use jquery to build an ajax call with all fields you need. Option two: use jquery to build a form with all fields, and submit it to the action. Best option: rewrite your partial views not to output a form, just the fields, and put the form element with the necessary action in the "main view".
 
Keep in mind: with mvc you have full control over the html code, you don't need to stick to the classic asp.net concepts.
  Permalink  
v2
Comments
Don Loukes at 14-Oct-12 17:52pm
   
Thank you very much Zoltan. I will rewrite the partial view as a user control - but I will still have almost the same problem (remember I am a newbee).
 
How do I:-
1. Embed it into a view?
2. Interact with the properties, methods and events of the user control from the jscript in the view on the client side?
Zoltán Zörgő at 15-Oct-12 0:32am
   
1) As you did already: @Html.Partial or @Html.RenderPartail or even Html.RenderAction (see: http://www.arrangeactassert.com/when-to-use-html-renderpartial-and-html-renderaction-in-asp-net-mvc-razor-views/)
2) You can not, since there is no user control there. But please distinguish between the view and the client side: the (partial) view is on server side, the on the client side is only html and javascript. So if you want to access the input fields rendered by a (partial)view form javascript (jquery), you can do it in the old-fashioned way: via it's name or element id. But to do that you have to provide the proper names and ids in the view, since you are controlling the rendering.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 Afzaal Ahmad Zeeshan 257
4 DamithSL 225
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,045


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 14 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100