【问题标题】:Partial view not rendering MVC razor部分视图不呈现 MVC 剃刀
【发布时间】:2012-04-13 15:49:44
【问题描述】:

这是我第一次使用局部视图,我无法获得实际的局部视图。 ajax 函数被调用,控制器被命中,并且 ajax 调用中的警报显示部分视图在那里。但是,将错误写入控制台(或警报)后,我的 div 仍然是空的。 我的应用程序是一个 MVC4 应用程序,但我很确定我只是在某个地方犯了一个愚蠢的错误,这不是 MVC 的错 :)

经过几个小时的谷歌搜索,我真的很高兴有人能帮助我完成这项工作,我非常感谢所有关于代码/ajax 的提示/cmets!

public PartialViewResult Groups()
{
        var person = _userRepository.GetCurrentUser();
        var connections = (from c in person.Person1 select c).ToList();
        var groups = _context.Groups.Where(g => g.GroupId == 1);

        var all = new GroupViewModel()
                      {
                          Connections = connections,
                          GroupDetailses = (from g in groups
                                            select
                                                new GroupDetails
                                                    {
                                                        Name = g.Name,
                                                        StartDate = g.StartDate,
                                                        StartedById = g.StartedById,
                                                    })

                      };
        return PartialView("Groups",all);
}

我的部分视图

@model Mvc4m.Models.GroupViewModel

<h2>Groups</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<h3>Create new Group</h3>
<div class="ui-widget">
    <label for="group">Groupname: </label>
    <input id="group" />
    <button onclick="addGroup()">Add</button>
</div>

@foreach (var item in Model.GroupDetailses)
{
    @Html.LabelFor(model => item.Name)<text> : </text>
    @Html.DisplayFor(model => item.Name)
}
<script>
    function addGroup() {
        $.get(
                "/Profile/AddGroup",
                {
                    Name: $("#group").val()
                });
        location.reload();
    };
</script>

我对 Profile/Index 的 Ajax 调用

@model Mvc4m.Models.ProfileView

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<div id="test" style="background-color:Aqua; width:200px; height:100px"></div>

<button onclick="load()"></button>

<script type="text/javascript">
function load() {
    $.ajax({
        type: "GET",
        url: '/Profile/Groups',
        dataType: 'html',
        success: function (response) {
            $('#test').html(response);
            alert(response);
        },
        error: function (xhr, status, error) {
            alert(status + " : " + error);
        }

    });
}

</script>

【问题讨论】:

  • 说真的,我无法在这个网站上获得正确的格式:( 我打算将代码部分用四个空格,怎么又变成了这样?
  • 控制台出现什么错误?

标签: ajax asp.net-mvc partial-views


【解决方案1】:

事实证明代码有效,重新启动 Visual Studio 就成功了!有时我多么讨厌VS……

【讨论】:

  • 您使用的是什么版本的 Visual Studio?
  • 2010 Ultimate,最近一天要和我“分手”几次:/
  • 呃。这不好玩。我一直在使用 Visual Studio 11 的 Beta 版本,它看起来非常稳定,所以你可能想尝试一下。顺便问一下,您的网站 (perfectdiet.com) 怎么样了?我只是收到“禁止”错误。
  • @EthanBrown 哦,重定向已关闭,网络酒店存在问题:/ 几个月前将网站移至 irisclasson.com/blog。我一直想试一试VS11,我想是时候了。昨天不得不强制关闭 VS 10 次,因为它不允许我编辑我的 cs 文件:/
猜你喜欢
  • 1970-01-01
  • 2011-10-10
  • 1970-01-01
  • 2013-06-17
  • 2021-03-14
  • 2018-04-24
  • 1970-01-01
  • 2017-01-28
  • 2020-01-26
相关资源
最近更新 更多