[英]jquery ajax load partial view to div tag MVC
I've been searching around with no luck on how to make a jquery call to load a partial view in a div tag on my Index view. 我一直在寻找有关如何进行jquery调用以在索引视图的div标签中加载部分视图的运气,但运气不好。 Some how I am not getting the partial view to update when I click on a link on my tree.
单击树上的链接时,我如何无法部分视图更新。 The partial view loads when I first run it b/c I call
<div id="divid"> @Html.Partial("_InnerView")</div>
. 第一次运行b / c时会加载局部视图,我称
<div id="divid"> @Html.Partial("_InnerView")</div>
。 After that nothing happens when I click on the link. 之后,当我单击链接时,什么也没有发生。 Or maybe I am not getting the full picture here.
也许我没有在这里看到完整的图片。 Some mentioned to use
$('#divid').load = data;
有人提到使用
$('#divid').load = data;
or $('#divid').innerHTML= data;
或
$('#divid').innerHTML= data;
but nothing works for me. 但对我没有任何作用。 This is what I have.
这就是我所拥有的。
Controller: 控制器:
public ActionResult Test(string parentEls)
{
if (Request.IsAjaxRequest())
{
Employee employee = new Employee();
employee.Name = parentEls + "1";
return PartialView("_InnerView", employee);
}
return View("_InnerView");
}
Index view: 索引视图:
<div id="divid">
@Html.Partial("_InnerView")</div>
$('#tree a').click(function () {
var parentEls = $(this).parents('ul').map(function () {
return $(this).find('a').first().text();
}).get().join(", ");
$.ajax({
type: 'POST',
url: '@Url.Content("~/Home/Test")',
data: {
parentEls: parentEls
},
success: function(data) {
$('#divid').innerHTML = data;
}
});
});
_InnerView.cshtml: _InnerView.cshtml:
@model TreeDemo.Models.Employee
EmpName:@Model.Name
UPDATE: I got this to work with this 更新:我得到这个与此
$.ajax({ url: '/Home/Test/', contentType: 'application/html; charset=utf-8', type: 'GET', dataType: 'html', data: { parentEls: parentEls } })
You have to use 你必须用
$('#divid').html(data);
instead of 代替
$('#divid').innerHTML = data;
Load Partial View in a div MVC 4 在div MVC 4中加载部分视图
Recently I want load Partal View in Div , after doing lots of R&D and It's work for me 最近,我经过大量的研发后想在Div中加载Partal View,这对我来说很有效
$.ajax({
type: 'POST',
url: '@Url.Content("~/ControllerName/ActionName")',
data: {
title: title
},
success: function(result) {
$('#divid').innerHTML = result;
}
});
And In Partal View Action Controller Code
public PartialViewResult ShowCategoryForm(string title)
{
Model model = new Model();
model.Title = title;
return PartialView("~/Views/ControllerName/PartalView.cshtml", model);
}
I think it can be useful if you check the request/response objects of your call, so you can see what is really happening after you make the call... As per your code, I can notice that you're posting using 我认为如果您检查呼叫的请求/响应对象会很有用,这样您就可以查看拨打电话后的实际情况...根据您的代码,我可以注意到您正在使用
$.ajax({
type: 'POST',
url: '@Url.Content("~/Home/Test")',
data: {
parentEls: parentEls
},
success: function(data) {
$('#divid').innerHTML = data;
}
});
but your action is marked for 'getting'... you'd need to have something like this 但是您的操作被标记为“正在获取” ...您需要输入类似的内容
[HttpPost]
public ActionResult Test(string parentEls)
so MVC can understand that the action should be called when HttpPost verb is used 因此MVC可以理解,使用HttpPost动词时应调用该动作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.