I have a web site written using ASP.NET MVC3.
The site has one single web page and what I want to do is to have the controller handle the request, do some parsing and processing and thereafter trigger a certain javascript method in the single webpage based on part of the data from the request.
How would I do this in the MVC controller?
You can't directly trigger actions in the View from the controller.
However, what you can do is return an ActionResult from the Controller Action to the View that instructs the View to execute your Javascript.
This could be a PartialView that includes the Javascript (in which case, you'll need a placeholder in your view for the PartialView to be rendered 'into') or something like a JSONResult that contains a property instructing the logic within the View what to do.
Either way, the call to the Controller Action is going to be triggered by client-side Javascript and your desired logic executed when the Controller Action finishes executing.
You'll probably find it easier to use jQuery.
Related
Basically I have a webpage structure where common parts of the page (header, side bar, etc) are generated in a separate controller filled with child actions outputting partial views. I then call these actions (using RenderAction()) from the layout page of the website.
So (if I'm right in saying this), There are multiple internal mvc pipelines (header/sidebar internal requests) including the original request pipeline to for the specific webpage. How/Where can I initialize some data from the original pipeline request and have that data accessible from the other internal mvc pipeline requests?
Summary of what I want to accomplish (with example)
Request for website comes in.
MVC starts pipeline for "Home" controller, "index" action.
Before the Action gets executed, some data needs to be created that can later be accessible.
From the layout page, several "RenderAction" Methods get executed creating sub pipelines for interal requests (e.g. "Shell" controller, "DisplayHeaderBar" action
"DisplayHeaderBar" needs to access some data that was set in step 3 before rendering partial view
Hopefully this makes sense...
What you're looking for are child actions. You simply create an action in some controller that returns a partial view. For example, you could handle your site navigation via:
[ChildActionOnly]
public ActionResult SiteNavigation()
{
// get the data for your nav
return PartialView("_SiteNavigation", yourSiteNavModel);
}
The ChildActionOnly attribute ensures that this action can only be called as a child action, making it inaccessible via typing a URL in a browser's navigation bar.
Then, you create a view in Views\Shared\_SiteNavigation.cshtml:
#model Namespace.To.ClassForSiteNavigation
<!-- render your site navigation using the model -->
Finally, in your layout:
#Html.Action("SiteNavigation", "ControllerWhereThisExists")
I think you could use Tempdata for that. Tempdate gets deleted after you access it, so if you want to use the data more then once use Tempdata.Peek or Tempdata.Keep.
Here is a link with some explanation how you can pass data in asp.net mvc.
https://msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspx
If tempdata doesn't do it then you could use cache.
I am using ASP NET MVC4, I want to use partial view to update my main view. On the server I get certain events from another server, and when that happens I want to update the partial view and post it to the client. How can I do that? In other words, I want a way to force rendering partial view on the server side without a postback request coming from the client. Is that possible, or the client must be informed first and then does its own postback action to trigger the partial view rendering?
Call The follwing Ajax call on every other server event or call it periodically
$.get('#Url.Action("ActionName", "ControllerName")', function (data) {
$('#Id of div where Partial View is renderred').html(data);
});
In the action called.
public ActionResult ActionName()
{
//load your model with changes
return PartialView("Name of your partial view", model);
}
This is a better job for SignalR for large scale Real-time work reference:
[http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-server][1]
Or if you just need to stay on single medium, you can look into node.js, which is Server side javascript and allows for binding of js function to server driven events (i.e. Client independent)
One last thought would be to use events and kick out Web Api Responses, but that would be getting a lot of weight on JSON without page requests.
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?
'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 ...
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.