简体   繁体   English

MVC3 C#Razorview使用Java脚本打印PartialView

[英]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. 在用户之上,我具有链接,用户可以单击以访问他的个人资料或他的报告等。我创建了一个链接,以使用Media Print CSS打印他们的个人资料,以使其在纸上看起来完美。

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 :(. Chrome只是说没有可用的视图,firefox没有响应,IE也没什么:(。

If you have any thoughts that would be great. 如果您有任何想法,那就太好了。

AJAX 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(). 好的,您输入一个location: url ,然后(一旦我想它已经进入了url),就调用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. 但是,该语句永远不会被击中,因为更改位置类似于return语句,即所有执行都在该点终止。

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). 是否有可能适合您的替代方案,即执行jquery ajax调用,或者jquery不适用(我看不到标签)。 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: 此外, @Url.Action() ,您的URL定义非常脆弱,您至少应考虑使用@Url.Action()即:

<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 ; 我的坚定建议是使用Ajax调用您的partialview there's no other way that this is going to work for you based on the info you've supplied. 根据您提供的信息,没有其他方法可以为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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