Same model instance for two partial views (ajax) - asp.net-mvc

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.

Related

Using MVC Partial View's own model instead of its parent's

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))

What is the best practices to track action id in controller for saved form?

Wasnt sure how to word the question, but this is the scenario:
the view is a data entry form eg http://127.0.0.1/User/AddEdit/
so edit the user I have an ID: http://127.0.0.1/User/AddEdit/7838fd9c-425c-4c98-b798-771bba10d9c1
This ID gets the data to populate the form values in a ViewModel, which populates the form
I am using jquery/ajax to save the form, which returns a Json result, indicating ok/error etc
In the View, I get the ID and use this in a hidden field which is set via jquery when the page loads and when the form is saved via ajax.
This seems a bit clunky, how do others do this?
in my opinion best solution is to create a partial view with all the fields and use it on add and edit view which are separate actions in controller. after you create user you can redirect to action edit. if you must / like use ajax you can reload div with form (change from user/add to user/edit/1). i might be wrong but i never see a code or example with one action in controller for add and edit.

PartialView updating ModelData?

am new to MVC but have a Q about PartialViews. I have two PartialViews accessing Model Data from the ParentView (Model Data passed through via the ParentView's Controller). The first PartialView is used to update (add/delete values) Model Data to the database. The second PartialView generates a document based on the ParentsView Model Data. The problem is that if the data is changed in the database by the first PartialView then the ParentViews Model Data is now out of date and hence the Second PartialView (referencing the ParentsView Model Data) is now working with out of date data.
I realise the above should be re-engineered to to better suite, however is there a way to make the updated Model Data available at the ParentView level for the second PartialView to reference?
Usually in order to update something into the database an HTTP request is sent to the server and the controller action performs the update and renders a view meaning that the whole page is reloaded and the model data updated.
If you perform an AJAX request to update the database then you might need to update the second partial view as well so that changes are taken into account.

ASP.Net MVC reusable form as RenderAction or RenderPartial

I'm looking for a best practice for embedding a form on multiple pages as a partial view.
I have a contact form I'm looking to embed on multiple pages on a site. Usually, the form would be on a contact page and the contact model could be the model for the view and use data annotations for validation. However, the view is already strongly typed.
How can I create a reusable form in a partial view and embed it on the page? I'm using N2 on the site, so the pages have to already have a strongly-typed model, but I would be open to extending those objects.
Personally, I recommend using for Html.RenderAction() for cross-cutting concerns such as these.
The handler for your contact form is going to need to exist independently of the page your are currently viewing so you are left with 3 options:
Manually add it to the response of
the current action
Manually add it to the response of
the current controller by way of a
base controller that modifies the
ViewState or ViewModel
Call the RenderAction()
HtmlHelper inside of the current view
Of these 3 options, while the third is technically more costly than 1 and 2 (because it initiates a brand new request), it is also the most maintanaible solution. By calling RenderAction() you have the advantage of being able to completely isolate your contact form from the rest of the view and thus you won't have to worry about hacking it into the current controller responses.
Use RenderPartial if data model for partial view is already in main view's model, in other case use RenderAction (then the action of the partial view will create its view model itself).

How can I validate partial view that is returned from controller

I have a view page with ajax.action link which returns a partial view from controller and render it to divid as updated target id. But I could not perform client side validation on that partial view.
Can I have solution for it?
When you load a partial view's html with ajax it is normal for the JavaScript code not to be executed. Especially if you have calls to functions attached to onload event since this event is fired long before the ajax call is executed. Check out this article http://adammcraventech.wordpress.com/2010/06/11/asp-net-mvc2-ajax-executing-dynamically-loaded-javascript/ it describes all sorts of problems that you can have with this approach. If you want a more specific answer it will be good to proivide more info on your setup like - what version of .net/asp.net mvc you are using and what validation framework are you trying to use.

Resources