ASP .NET MVC partial views and routing - c#

I have an MVC view that contains a number of partial views. These partial views are populated using partial requests so the controller for the view itself doesn't pass any data to them. Is it possible to reload the data in one of those partial views if an action was triggered in another? For example, one partial view has a jqGrid and I want to refresh the data in another partial view when a user selects a new row in this grid.
Is there a code example for this scenario (in C#) that I can look at to see what am I doing wrong? I am using ajax calls to trigger a new request but non of the partial views are refreshed so I am not sure if the issue is with the routing, the controller, or if this even possible at all!
Thanks!

If your partial view action returns a ViewResult, the response contains an HTML chunk. However, an Ajax call does not automatically insert the result into the DOM, as the result can be in any number of formats and/or might need additional processing before the DOM is updated. So, in order to get your partial view refreshed, you need to take the result of the Ajax call and insert it in the right place in the DOM tree.
jQuery has a nifty load() method, that will encapsulate this process for you. It'll make the Ajax call, take the result and replace the inner HTML of the selected element. You can even pass it your own function to be executed before it inserts it in the DOM tree, if you need to transform the result in any way.
Side note: jQuery.load() strips scripts returned in the result. It does retain them for execution in certain sceanrios, but it does not even execute them in other scenarios. So, if your partial view contains scripts, you might consider updating the DOM tree yourself.
All that this has nothing to do with your C# code, which runs on the server side.
However, you do have the ability to make your action a little bit smarter by checking if the request is plain HTML, so the result will be rendered by the browser directly, or if it's an Ajax call and the result will be processed by your script before getting in the DOM. This check is done using the Request extension method IsAjaxRequest.

Related

Returned View does not open as a separate view in MVC 4

I have a main view which is having a partial view placed inside a ajax form. Upon the form submission I am returning another View but the problem is that the returned view is rendered in the earlier loaded main view only. It does not opens as a separate view.
I think that the problem could be due to the use of ajax form which is calling the controller action method but I am not sure.
Any help would be appreciated .
This is the expected behavior with ajax. Btw you are not using Ajax, but Ajah as you are returning HTML and not XML/JSON.
Ajax will not open a new page or popup without writing javascript code. Ajax is a way to transmit data between the current page and the server.
If you use Ajax.BeginForm, you can specify the ID of the dom element in which the HTML returned from your action should be attached.
If you use Html.BeginForm, you are not using ajax at all.

How to pass a list from Controller to View?

This is what I've tried:
In Controller:
public ActionResult Detail()
{
List<string> list = new List<String>();
return View(list);
}
In View of Detail:
#model dynamic
<!-- some HTML -->
<script>
#Model.add(document.getElementById("input_box").value);
</script>
Problem is, intelli-sense is not detecting that the passed in object is a List so #Model.add doesn't work.
My goal is to pass a list into the view so I can gather data from user inputs, and then use that list of data in my controller. Any ideas?
The current problem can be solved by using a statically typed model:
#model List<string>
<!-- some HTML -->
<script>
#Model.Add(document.getElementById("input_box").value);
</script>
However, you'll quickly hit another wall when the compiler complains that it doesn't know what document is. That's because the Razor view is rendered on the server side, but document is an object provided by the browser on the client side. You simply can't dynamically add elements to this server-side list from the client. You'll need to encode your list somehow for the client to be able to read, say in a JavaScript array. Then dynamically modify the list on the client side, perhaps using the JavaScript .push method. And then post this back to the controller in a separate action.
It's not going to be that simple.
Firstly, the Razor statement you used
#Model.add(document.getElementById("input_box").value);
is not a valid C# statement. It will cause an exception when you try to open the page, because the Model type you're using (List<T>) does not have a method called add (though it has Add), and further because that javascript is not valid Razor code either :)
Secondly, you seem to be confused about how the server-client interaction works.
You need to carefully think about how what you want to do can be realized--
For you to be able to get information from the client (the user's browser), you first need to render the page, send it to the client, let the user (or whatever) enter the information, and then get them to send it back to a Controller action. At the moment your page is not even rendering, so you couldn't hope to get data from anyone.
This can still be solved in numerous ways, one of which is to use Html.BeginForm(...) to create a simple form and have an input element and a submit button somewhere in there.
If you can get the user to submit the data back to you, you can then do whatever you like with the data in the previously mentioned Controller action.
Okay, solved it by creating a global array in the JavaScript of the View, then passing it to a Controller function as a parameter with Ajax.

Display form in pop up

i have two cshtml page one which has the link the makes the popup appear, and another with just the form data and would like to know how i would be able to display the form on the popup, as i am using MVC, to create the form, the plage which has link on is in the ~/views/client/index.cshtml while thr form is ~/views/fb/CreateOrEidt.cshtml the colller is called "FB" and the methos to call is edit, it take the parameter Id
I have tried #hmtl.renderpartial, #html.renderaction, #hmtl.partial, #html.action,
I have also tied these method with a { after the # and the end, doesnt give me a error bust still doesn't display information
The error which i get is razor canot convert type object to void
It's a little unclear what you're trying to achieve but from the error you've mentioned when calling Html.RenderAction you need to call using something like:
#{
Html.RenderAction("ACTIONNAME");
}
Calling the other methods against an action view won't work.
If you're trying to display a view inside a popup though you want to be careful that you don't end up including your layout page etc as this is potentially not the look you're going for..!
The pattern I generally implement for things like this is to simply partial out the form bit that I need to reuse and call renderpartial to display this where I need it. This renders just the html I'm after then and not the whole view (+ layout(s)).

Asp.Net MVC and Partial Views and Mixing Ajax.BeginForm and Html.BeginForm

I have a some partial views that generally make use of small model data. I have been using Ajax.BeginForm to with js functions defined in the AjaxOptions to catch the partial postback's success or failure when that part is submitted.
I saw no performance penalty as my data has been light. Currently, I have been working on a piece that returns an HTMLFragment from an SSRS report rendered as format "HTML40" in the controller. The JSON serialization of the html portion is taking a long time and I no longer see it as an option. When I changed the Ajax.BeginForm to Html.BeginForm and rendered the report upfront then the payload is lighter and takes less time to render but now I am stuck. How do I issue refresh commands without loading the partial again? This is why I used Ajax.BeginForm in the first place :( Perhaps there is a better way to load the data than what I am doing. I have started looking into callbacks and somehow getting the report pieces a raw byte[] or string as opposed to using json serialization.
Thanks
You can load a whole partial into a div using .load() jQuery function:
$("#someButton").click(function(){
$("#someDive").load("/url", {type: 'POST'}, function(){
});
});
Now, when you click on #someButton, an AJAX call will be made to /url. The /url can return a PartialView that will be loaded directly into your div:
//controller URL
[HttpPost]
public ActionResult Index(){
return PartialView("MyPartialView");
}
You can pass in models as you'd do to regular view.

How can I set a variable in my pages javascript based on my controller action in ASP.NET MVC?

So, I have a page that looks like the following:
alt text http://img704.imageshack.us/img704/5973/croppercapture3.png
This is my basic messaging inbox. I have 2 pages right now, Inbox and Sent (there will eventually be others). These both share the same View, and just have 2 different actions to populate the appropriate data.
The elements on the right-hand side (the message list) is a partial view which has its data populated based on my two controller actions Inbox(int? page) and Sent(int? Page).
So, I have one view "MessageView" and one partial view "MessageList" shared between Inbox/Sent.
However, I now have to get those arrow buttons ("<" ">") working using Ajax. I know how to use jQueries ajax calls well enough, and I know how to render the result of the action call (which returns a partial view).
The problem comes from the fact that the javascript that makes these pagination ajax calls needs to know two things:
What the current page is (whether it be /messages/inbox or /messages/sent)
What the current page is (specified in the query string, ie /messages/inbox?page=2).
Without knowing which page I'm on (Inbox or Sent), it wont know which url to make the ajax call on. Should it make the postback to /messages/inbox or to /messages/sent?
If I wasn't making these messages load with Ajax it would be as simple as loading the appropriate url into the link tags for the "<" and the ">" buttons. But I can't, because part of my requirements states that it must load the messages below without visibly refreshing to a new page.
In JavaScript you can check window.location.pathname to see the pathname section of the current’s page’s URL.
window.location.search gives you the query string.
When the user clicks the Inbox or Sent buttons, you need to rewrite the URLs in your arrows so that they point to the right place.

Categories

Resources