ajax - 局部 View 不渲染 MVC Razor

标签 ajax asp.net-mvc partial-views

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

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

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

我的局部 View

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

我对配置文件/索引的 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>

最佳答案

结果代码有效,重新启动 visual studio 成功了!有时我多么讨厌 VS...

关于ajax - 局部 View 不渲染 MVC Razor ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9934858/

相关文章:

javascript - Ajax 的页面滚动效果 数据如何加载到页面中

asp.net-mvc - Facebook 实时更新不发布数据

c# - 在没有查询字符串的情况下将变量从 View 传递到 Controller

c# - 在数据表中呈现 HTML

asp.net-mvc-3 - 是否可以在 ASP.NET MVC 中的多个项目上重用部分 View ?

c# - 如何从mysql数据库中提取固定数量的项目

javascript - AJAX 从 cfc 返回变量

java - 如何在 Spring MVC/WebFlow 项目中进行 AJAX 调用

ASP.NET MVC3 嵌套部分 View ?

ruby-on-rails - 在 Rails 中将部分从一个 Controller 的 View 渲染到另一个 Controller 的 View