[英]Load partial view in a div MVC
I have a button on my MVC view on click of it, it should add a partial view in a 'div', by calling an action which takes an object as a parameter单击它时,我的 MVC 视图上有一个按钮,它应该通过调用一个以 object 作为参数的操作在“div”中添加一个局部视图
I tried out some thing like this:我尝试了这样的事情:
$('#buttonId').onclick(function(){
$('#divid').load(@Html.Action("ActionName","ControllerName",new{parameterName = objectToPass}))
});
but it loads the actionresult/partial view on page load itself not a click of button但它会在页面加载本身而不是单击按钮时加载操作结果/部分视图
Any Idea?任何想法?
Try to use 尝试使用
@Url.Action
instead of 代替
@Html.Action
Or you can use ajax, for example: 或者您可以使用ajax,例如:
$('#buttonId').click( function() {
$.ajax({
type: 'POST',
url: '@Url.Content("~/ControllerName/ActionName")',
data: objectToPass,
success: function (data) {
$('#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 最近我想在进行大量研发后加载部分视图,这对我来说很有用
$.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);
}
<script type="text/javascript">
$(document).ready(function () {
$('#buttonId').click(function () {
$('#divid').load('@Url.Action("About", "Home")');
});
});
</script>
<a id="buttonId">Load partial view or Any URL</a>
<div id="divid" style="height:100px; width:400px";>
</div>
load require a string, you are generating a variable path if you look at your source code it generate something like: load需要一个字符串,如果你看一下你生成的源代码,就会生成一个变量路径:
$('#buttonId').click(function(){
$('#divid').load(yoururl/);
});
but what you want is: 但你想要的是:
$('#buttonId').click(function(){
$('#divid').load("yoururl/");
});
the code should look like: 代码应如下所示:
$('#buttonId').click(function(){
$('#divid').load('@Html.Action("ActionName","ControllerName",new{parameterName = objectToPass})');
});
but as I can see the load should be working until you click on #buttonId
但正如我所看到的那样,负载应该正常工作,直到你点击
#buttonId
On ajax call success function append result 在ajax调用成功函数追加结果
ajax call=> ajax call =>
url: 'controllername/methodname'
success: function (partialviewresult)
{
$('#div').append(partialviewresult);
}
// inside controller
public ActionResult methodname()
{
return PartialView("~/a/a.cshtml");
}
and controller must return partialview as result 和控制器必须返回部分视图作为结果
Instead of innerHTML, html(data) worked for me; html(data) 代替 innerHTML 为我工作;
$('#buttonId').click( function() {
$.ajax({
type: 'POST',
url: '@Url.Content("~/ControllerName/ActionName")',
data: objectToPass,
success: function (data) {
$('#divid').html(data);
}
});}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.