I have an observable that gets modified in the main view. In a "Preview" below, I render an MVC partial (this preview is re-used in many places throughout the app) displaying some of that observable data in real-time as it gets updated.
When I call the partial:
#Html.Partial("_TitlePanel", Model)
How can I also pass a knockout / js object to the Partial?
You don't need "pass a knockout js" in your partial view. On the client side, no main and partial views, only one html page. So, you should add data-binding in your partial view, like as in main view.
Related
I need to populate a list box in a partial view, using ASP.NET MVC4.
Can Partial View have its own #model, as opposed to taking a model from its parent as described here?
I can populate my dropdown box using a separate AJAX call to another MVC controller (i.e. not parent page/url) as discussed here, but the resulting syntax is a bit more clumsy; furthermore, an additional endpoint is exposed to the outside world.
Yes - you can call partial view with any model you like. There is no requirement that data somehow comes from current model.
#Html.Partial("PartialView1", new MyOtherModel(42))
For best performance, it is best practice to place Javascript code at the bottom of a page. Now I have a Partial View in MVC which I am loading using the Html.Action method. I am also using the OutputCaching attribute in order to cache the controller action response and hence speed up server response. This is my issue:
Within such a partial view, I have some Javascript which initialises a javascript carousel. The ID of the carousel element is generated on the fly within the same Partial View itself. In order to gain the best performance, the javascript code is 'registered' within the ViewContext and then rendered at the end of the page.
Now since I have OutputCaching enabled, the registration of such javascript code with the ViewContext is done only once when the item is not cached cause else, no logic is executed.
A workaround to this solution is not to include the javascript code at the end of the page but as part of the PartialView itself and hence it is cached with the entire content of the Partial View.
Is there anything you can suggest to cache the Partial View and register the Javascript code at the end of the page?
Here's what happens:
Partial views A and B show the same model
View A changes the data via Ajax (using Ajax.BeginForm)
View A re-renders itself (controller gets the updated model from the database)
Now we have to re-render View B as well, because the data has been changed, right?
We get the updated model from the database again and re-render View B
How do I prevent re-querying the database? Maybe cache the model-instance in the Session?
What's the "right" way to do this?
<!--works great when the page is rendered
via postback but what about Ajax?--!>
<div>
#Html.Partial(#ViewA", MyModelInstance)
#Html.Partial(#ViewB", MyModelInstance)
</div>
Can you create a new partial View C that contains both A and B. Whenever the model changes you have to invoke the action that returns the View C.
UPDATE:
The other simple solution I see is when View A updates the model instead of re-rendering the view, get the updated model through AJAX and the update HTML portions through javascript. If you are using jquery you can use the template plugin to update the html quite easily.
By this way you can avoid making one more unnecessary request to update the other View B.
I have a concept of a modal window in my MVC application that renders some HTML that wraps content that will eventually show inside on the modal. The content will be HTML either directly hard coded in the view or generated by an Html helper as passed in as a parameter.
I'd like to wrap the modal content in a Razor template or partial in my application to avoid spreading it all over my application as it used in a number of pages. What's the easiest way of doing this? Can I achieve something similar in i partial view with out a model?
Build a viewmodel for the partial view... a simple C# class with a string property for the Html... using the [AllowHtml] attribute on it will let you stuff HTML in the object.
In your parent view, stuff your HTML into this view model and pass it to the partial when you call it.
'm using ASP.NET MVC 3 with Razor views. I have a partial view which I would like to render on all pages so I am placing it in the site's main Layout page. However, I am not sure the best way to load data into the partial view. I could load it for each ActionMethod but there is there a way to do this globally across the entire app?
Move all the data loading logic for your partial into a separate action method.
Then in your layout page, instead of rendering the partial with a call to RenderPartial(), call the RenderAction() method.
RenderAction() makes a "child" action call - thus putting all the logic needed for that partial into one place.
Write action for this partial view in MasterController because every controller inherits from it, and place your partial view in shared folder and call it on Site's master page (like every site has a user control which gives login box until user is logged in else it displays logged in user info) ... hope it answer your ques ...