Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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.
 
Share this answer
 
v2
Comments
Don Loukes 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ő 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900