Ajax 不渲染MVC razor的局部视图
这是我第一次使用局部视图,我无法获得实际的局部视图。ajax函数被调用,控制器被命中,ajax调用中的一个警报向我显示partialview在那里。但是,当错误写入控制台(或警报)时,我的div仍然是空的。 我的应用程序是一个MVC4应用程序,但我很确定我在某个地方犯了一个愚蠢的错误,这不是MVC的错:) 在谷歌上搜索了几个小时后,我真的很高兴有人能帮我完成这项工作,我非常感谢所有关于代码/ajax的提示/评论Ajax 不渲染MVC razor的局部视图,ajax,asp.net-mvc,partial-views,Ajax,Asp.net Mvc,Partial Views,这是我第一次使用局部视图,我无法获得实际的局部视图。ajax函数被调用,控制器被命中,ajax调用中的一个警报向我显示partialview在那里。但是,当错误写入控制台(或警报)时,我的div仍然是空的。 我的应用程序是一个MVC4应用程序,但我很确定我在某个地方犯了一个愚蠢的错误,这不是MVC的错:) 在谷歌上搜索了几个小时后,我真的很高兴有人能帮我完成这项工作,我非常感谢所有关于代码/ajax的提示/评论 public PartialViewResult Groups() {
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>
@model Mvc4m.Models.GroupViewModel
组
@ActionLink(“新建”、“创建”)
创建新组
组名:
添加
@foreach(Model.groupDetails中的var项)
{
@Html.LabelFor(model=>item.Name):
@DisplayFor(model=>item.Name)
}
函数addGroup(){
美元(
“/Profile/AddGroup”,
{
名称:$(“#集团”).val()
});
location.reload();
};
我对概要文件/索引的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>
@model Mvc4m.Models.ProfileView
@{
ViewBag.Title=“Index”;
}
指数
函数加载(){
$.ajax({
键入:“获取”,
url:“/Profile/Groups”,
数据类型:“html”,
成功:功能(响应){
$('#test').html(响应);
警报(响应);
},
错误:函数(xhr、状态、错误){
警报(状态+”:“+错误);
}
});
}
原来代码是有效的,重新启动visual studio就成功了!我多么讨厌VS有时…说真的,我无法在这个网站上正确设置格式:(我打算用四个空格来表示代码部分,为什么结果又是这样呢?控制台中的错误是什么?您使用的是什么版本的Visual Studio?2010 Ultimate,它每天都会与我“分手”几次:/Ugh。这一点都不好玩。我一直在使用Visual Studio 11的测试版,它看起来非常稳定,所以您可以I don’我不想试试。顺便问一下,你的网站(perfectdiet.com)怎么样了?我刚收到一封“禁止”的信错误。@EthanBrown噢,redrect已关闭,网络酒店出现问题:/几个月前将网站移至irisclasson.com/blog。我一直想尝试VS11,我想是时候了。昨天不得不强制关闭VS 10次,因为它不允许我添加cs文件:/