【问题标题】:Partial view data not showing MVC 4部分视图数据未显示 MVC 4
【发布时间】:2013-03-26 02:25:59
【问题描述】:

我认为这是我的 HTML / CSS 代码的问题,因为这些是我明显的弱点。我有一个部分视图,我只想在有人从下拉框中选择一个选项时显示。这个局部视图 (_ChampionStats) 正在加载,我可以在其中放置一个断点并看到它被击中,但数据从未显示。有什么建议吗?

Index.cshtml:

@using LoLBuild.Models
@model ChampionViewModel

<script type="text/javascript" src="~/jquery.js"></script>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Choose Your Champion</h2>
<div class="inputblock" style="clear: both;width:100%;">
    @Html.LabelFor(model => model)
    @Html.DropDownList("ChampionList", (IEnumerable<SelectListItem>)ViewBag.ChampionSelectList, new { @onchange="location = Index/this.value;" })
</div>

<div id="ChampionStats">
    @if (ViewBag.SelectedChampion > 0)
    {
        @Html.Partial("_ChampionStats", Model);

    }
</div>

<script type="text/javascript">
    $(document).ready(function() {
        $("#ChampionList").change(function() {
            var strSelected = "";
            $("#ChampionList option:selected").each(function() {
                strSelected += $(this)[0].value;
            });
            var url = "/Champion/Index/" + strSelected;

            $.post(url, function(data) {
                // do something if necessary
            });
        });
    });
</script>

部分视图 (_ChampionStats.cshtml):

@using LoLBuild.Models
@model ChampionViewModel

@{
    ViewBag.Title = "ChampionStats";
}
    <h3>Partial Rendered Successfully</h3>
    @Html.TextBox("Health:", Model.BeginHealth, new {style = "width: 100px;"})

编辑:我注意到控制台中有一个错误,但我不确定这意味着什么。这是错误:

GET http://localhost:49219/jquery.js 404 (Not Found) localhost:8
Uncaught ReferenceError: Index is not defined localhost:36
onchange

【问题讨论】:

    标签: html asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview


    【解决方案1】:

    【讨论】:

    • 我尝试了这个,但没有产生任何结果。我仍然能够在局部视图中放置一个断点,并看到它实际上被调用并且视图被找到但屏幕上没有出现任何内容。这是我对索引视图所做的代码更改:
      @if (ViewBag.SelectedChampion > 0) { Html.RenderPartial("_ChampionStats", Model); }
    【解决方案2】:

    404 表示路径 http://localhost:49219/jquery.js 不存在。 jquery 的默认位置是脚本文件夹,并包含版本,即http://localhost:49219/scripts/jquery-1.8.3.js。您可以使用浏览器的 f12 中的工具来检查是否找到了文件,即 chrome 中的网络,或者 fiddler 会为您提供更多信息 蒂姆

    【讨论】:

    • 是的,这是错误之一,现在已解决。不幸的是,我只是不知道该放什么:new { @onchange="location = Index/this.value;" }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多