如何打印PartialView

时间:2019-05-28 06:26:48

标签: c# asp.net asp.net-mvc

我一直在寻找这个问题,并试图修复了几天,但没有成功。我在这段代码中做什么错?我要打印_Champions_SearchEnginePartialView中的Home/Index public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpGet] public ActionResult _SearchEngine() { SearchLeagueModel searchLeagueModel = new SearchLeagueModel(); searchLeagueModel.MainSelector = "champions"; return PartialView("_SearchEngine", searchLeagueModel); } public ActionResult _SearchEngine(SearchLeagueModel searchLeagueModel) { if (searchLeagueModel.MainSelector == "champions") { return RedirectToAction("_Champions", searchLeagueModel); } else return View(); } [HttpPost] public ActionResult _Champions(SearchLeagueModel searchLeagueModel) { string chooser = searchLeagueModel.MainSelector; string selector; if (searchLeagueModel.MainSelector != null) { selector = "filter[name]=" + searchLeagueModel.MainSelector; } else { selector = ""; } WebRequest request = WebRequest.Create("https://api.pandascore.co/lol/" + chooser + "?" + selector + "&token=mytoken); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string responseFromServer = reader.ReadToEnd(); List<ChampionsModel> champions = JsonConvert.DeserializeObject<List<ChampionsModel>>(responseFromServer); return PartialView("_Champions", champions); } //other views } ,但显示错误。

我的HomeController

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace LeagueAPI.Models
{
    public class IndexModelView
    {
        public IndexModelView()
        {
            ChampionsList = new ChampionsModel();
            Searcher = new SearchLeagueModel();
        }
        public ChampionsModel ChampionsList { get; set; }
        public SearchLeagueModel Searcher { get; set; }
    }

    public class ChampionsModel
    {
        public List<string> videogame_versions { get; set; }
        public double spellblockperlevel { get; set; }
        public double spellblock { get; set; }
        public string name { get; set; }
        public double mpregenperlevel { get; set; }
        public double mpregen { get; set; }
        public double mpperlevel { get; set; }
        public double mp { get; set; }
        public double movespeed { get; set; }
        public string image_url { get; set; }
        public int id { get; set; }
        public double hpregenperlevel { get; set; }
        public double hpregen { get; set; }
        public double hpperlevel { get; set; }
        public double hp { get; set; }
        public double critperlevel { get; set; }
        public double crit { get; set; }
        public string big_image_url { get; set; }
        public double attackspeedperlevel { get; set; }
        public object attackspeedoffset { get; set; }
        public double attackrange { get; set; }
        public double attackdamageperlevel { get; set; }
        public double attackdamage { get; set; }
        public double armorperlevel { get; set; }
        public double armor { get; set; }
    }
}

我的IndexModelView

@using LeagueAPI.Models
@model System.Collections.Generic.List<ChampionsModel>
<fieldset>
    <div class="d-flex p-3 w-auto h-auto">
        @foreach (var element in @Model)
        {
            <div class="p-3 flex-lg-wrap">
                <div class="card p-2" style="width: 15rem ;">
                    <img class="card-img-top" src="@Html.DisplayFor(model => element.big_image_url)">
                    <div class="card-body p-0 m-0">
                        <h5 class="card-title p-0 m-0 text-center">@Html.DisplayFor(model => element.name)</h5>
                        <p class="card-text p-0 m-0">DMG/LVL: @Html.DisplayFor(model => element.attackdamageperlevel)</p>
                        <p class="card-text p-0 m-0">ARMOR/LVL: @Html.DisplayFor(model => element.armorperlevel)</p>
                        <p class="card-text p-0 m-0">MOVEMENT SPEED: @Html.DisplayFor(model => element.movespeed)</p>
                        <p class="card-text p-0 m-0">ATTACK RANGE: @Html.DisplayFor(model => element.attackrange)</p>
                        <a href="/Home/Details"
                           class="btn btn-primary m-1 "
                           OnClick="GreetingBtn_Click">More details</a>
                    </div>
                </div>
            </div>
        }
    </div>
</fieldset>

我的_Champions.cshtml PartialView(作为PartialView制造)

@model LeagueAPI.Models.IndexModelView

@Html.Action("_SearchEngine", Model.Searcher)
@Html.Action("_Champions", Model.ChampionsList)

索引视图

_Layout

其他一切看起来都很标准,在@RenderBody()中,我有_Champions Controller 我的第二个问题是关于IndexModelView indexModelView = new IndexModelView()。为什么当我将ActionResult作为参数并对此MainSelector进行必要的代码更改时,在这种情况下setwd("~/Desktop") causes<-read.csv('causes.csv') head(causes) table(causes$Intentional.self.harm..suicide) barplot(table(causes$Intentional.self.harm..suicide)) barplot(table(causes$Intentional.self.harm..suicide), ylab='Frequency', main='Barplot of Intentional self-harm (suicide)', col='lightblue' dput(head(causes, 20)) Intentional.self.harm..suicide. = c(535L, 579L, 480L, 541L, 499L, 537L, 466L, 453L, 459L, 494L, 520L, 553L, 525L, 588L, 578L, 631L, 676L, 656L, 757L, 673L) 为空。 我仍在学习,如果您能在这里解释出什么问题我将很感激。欢呼

2 个答案:

答案 0 :(得分:1)

我会说不是ActionResults而是在Action _Champions和_SearchEngine上都使用PartialViewResult

[HttpPost]
public PartialViewResult _Champions(SearchLeagueModel searchLeagueModel)
{
       return PartialView("_Champions", champions);
}

然后从索引视图呈现这些操作如下所示:

@model LeagueAPI.Models.IndexModelView
<div>
@Html.RenderAction("_Champions");
@Html.RenderAction("_SearchEngine");
</div>

答案 1 :(得分:0)

尝试以下Java脚本:-

<script>
function printContent(el){
    var restorepage = document.body.innerHTML;
    var printcontent = document.getElementById(el).innerHTML;
    document.body.innerHTML = printcontent;
    window.print();
    document.body.innerHTML = restorepage;
}
</script>