简体   繁体   中英

MVC3 C# Razorview Printing a PartialView using Javascript

I'm trying to have a button print a partial view.

| Mainview | Partial View |

| Users | Profile |


On top of users I have links the user can click on to access his profile or his reports etc... I created a link to print their profile with a Media Print CSS to make it look perfect on paper.

The issue I'm having is making it work.

function print_url () {
  location: url;
  print();
}
<li data-username="@Model.UserName"><a href="javascript:print_url('URL/Home/ContentView?username=@Model.UserName')">Print Profile</a></li>

This has to work for all browsers. Chrome just says no view available, firefox doesn't respond and IE nothing :(.

If you have any thoughts that would be great.

AJAX

getUserData: function (elem) {
  $elem = $(elem).closest("[data-username]");
  var username = $elem.data("username");
  var profile = $elem.data("profile");
  var directReport = $elem.data("direct-report");
  $.ajax({
    url: 'Home/ContentView?username=' + username,
    type: 'GET',
    data: username,
    data: profile,
    data: directReport,
    beforeSend: function () {
      $('#loader').css('display', 'block');
    },
    success: function (result) {
      $("#content").html(result).scrollTop(result);
      //$("#tabs").fadeIn('1500');
      //console.log(result + '\n\n DONE');
    },
    error: function (e, msg) {
      //console.log(e, msg);
    }
  })

Outcast,

Ok, you enter a location: url , then (once i imagine it's gone to the url), you call print(). However, that statement will never be hit as changing location is akin to a return statement ie all execution is terminated at that point.

Is there perhaps an alternative scenario that would work for you ie doing a jquery ajax call, or is jquery not applicable (i see no tags). We're gorra move fast here!! :-)

Also, as an aside, your URL definition is very very fragile, you should at minimum consider using @Url.Action() ie:

<a href="javascript:print_url('@Url.Action("action", "controller, new {username=@Model.UserName}, null)'">

... instead as this will guarantee fidelity no matter what your hosting config is like. Tho', my firm advice is to use ajax to call your partialview ; there's no other way that this is going to work for you based on the info you've supplied.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM