views:

121

answers:

3

Hi

Basically I have this

In my Asp.net controller

public ActionResult RenderMyView()
{
  data = // some data to used in rendering my partial view
  return PartialView("PartialView", data);

 //edit 
  I also return a json result if an error occurs. Say if I get an SQLException I will return  a json result back  and not render any partialview.
}

My ParitalView renders a table and the "data" is basically columns from the database. So it takes the information in the data my generates a table with that many rows.

This is in my java script file and is activated when a drop down list item is changed.

$.getJSON('RenderMyView', { 'field': field}, function(response)
{
    $('#id').after('<div>' + response + '</div>');
});

So what this does is calls RenderMyView what renders a partialView and adds the response code to my page.

So this is my thinking. It goes to my controller and renders and returns that PartialView. Once it gets rendered it is just html. So it is sending back html and then I add that html code into a div.

This works as I expect on Firefox but no other browser works. I tired Opera, Google chrome, IE 8, Safari none of them work.

I have no clue why since I don't have my trusty firebug.

I tired to use IE 8 firebug clone but for some reason it does not go into this part

$('#id').after('<div>' + response + '</div>');

I put break points on every line but after it goes to the server its like thats the end of the debugging it never goes back to the debugger and no errors are occurring on the server side since I am walking through it with the VS2008 debugger and nothing crashes.

Edit

It seems to not go into the response part at all. Like I put an alert box there and it never gets triggered.

A: 

I favour the call;

$.post("/Home/jQueryGetCalendarItems", { month: this.id }, function(retHTML) {

which works in FF and IE no probs. In the code above I'm returniong a PartialView and then replacing the contents of a div like you are.

EDIT

The other thing to look out for is the use of #. Your control name may be different than you expect. Do a [View/Source] to confirm control Id's etc. I prefer the use of '.ClassName' myself.

griegs
A: 

Shouldn't you be using $.ajax() rather than $.getJSON() if you're wanting to receive HTML? Does doing this make any difference?

$.ajax({
    url: 'RenderMyView',
    data: {field:'field'},
    dataType: 'html',
    success: function(response, textStatus) {
        $('#id').after('<div>' + response + '</div>');
    }
});
jammus
See the comment I put under "the out edges" post.
chobo2
+1  A: 

Is your partial view returning an JSON result? If not, your call is probably not working because the other browsers are expecting a JSON result and receiving Html/text result.

If your partial view is returning Html then you need to use an jQuery Ajax call using.

$.ajax()

or, if you are then going to dump the result in a div, I like to use

$("#Id").load("/RenderMyView/");

which runs an Ajax call and dumps it into the selected element

EDIT

In your controller you should catch the error and return a content result which contains the error message.

public ActionResult RenderMyView()
{
  try
  {
    data = // some data to used in rendering my partial view
    return PartialView("PartialView", data);
  }
  catch(Exception e)
  {
    //catches any errors returns it back as plain text
    //log the error that was thrown
    return Content("Error: " + e.Message);
  }
}

The Content just returns plain text from the controller. You will then need to check for this result in your JavaScript.

$.ajax({
  url: 'RenderMyView',
  data: {field:'field'},
  dataType: 'html/text',
  success: function(response) {
    if (response.indexOf("Error:") == 1) {
      //handle error
    }
    else {
      $('#id').after('<div>' + response + '</div>');
    }
  }
});
theouteredge
No my partial View returns no json.However what is not shown in my example code is that if something in that method fails. Say an SQLExecption occurs I return a json result. So I am sometimes return a json result and sometimes a html result. How can I change it so other browsers understand this?
chobo2
Could I maybe render the partial view in the controller and store it into a string and send it back as a json result?
chobo2
I think you have to do one or the other, I've never seen anyone do both. Why do you need to return a json result if their was an error? Couldn't your partial view just return an error message instead? If you want to do something if an error is returned in you JavaScript you could just look for your error text in the returned html.
theouteredge
Hmm sucks that firefox can handle that. Well what is happening is I have different types of error messages. So I would have a flag for each one. Like say "Failed" would be the first json result the next would be the error message. Now I know if my partial view failed or not. Also it is so I can write my error messages on the serverside and keep them in the same place and have it easy to change. I am not sure how how I could make the partial view return the error. Unless I make a totally new partial view and send one that has the html table or one that send just that error back.
chobo2
Do you have a controller action for this? I think the logic for this is best placed in a controller/action. It can then trap and log any errors that occur and decide what view to return, one with data or an error message.
theouteredge
I am not sure what is a controller action?
chobo2
I just reread you question, the controller action is your function "public ActionResult RenderMyView()" which you have already. I'll edit my response to show you what I mean.
theouteredge