簡體   English   中英

如何打印PartialView

[英]How to print PartialView

我一直在尋找並嘗試修復幾天,但沒有成功。 我在這段代碼中做什么錯? 我想在Home/Index _Champions_SearchEngine打印為PartialView ,但顯示錯誤。

我的家庭控制器

 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
}

我的IndexModelView

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; }
    }
}

我的_Champions.cshtml PartialView(作為PartialView制作)

@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>

索引檢視

@model LeagueAPI.Models.IndexModelView

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

一切看起來都很標准,在_Layout我有@RenderBody()我的第二個問題是關於_Champions Controller 為什么當我將IndexModelView indexModelView = new IndexModelView()作為參數並將必要的代碼更改引入此ActionResultMainSelector在這種情況下MainSelector為null。 我仍在學習,如果您能在這里解釋出什么問題我將很感激。 干杯

我會說不是ActionResults而是在Actions_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>

試試這個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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM