asp.net-mvc 单击按钮时呈现局部视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25489997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Render partial view on button click
提问by Alen Kopic
so I've tried everything, but I simply can not return a small partial view and place it in div element. This is my parent view
所以我已经尝试了一切,但我根本无法返回一个小的局部视图并将其放置在 div 元素中。这是我的父母观点
<button id="doctors">Doktori</button>
<button id="apointments"> Pregledi</button>
<div id="someDiv">
</div>
<script>
document.getElementById("doctors").onclick = function () { myFunction() };
function myFunction() {
$("#someDiv").load("@Html.Raw(Url.Action("doctorsview", "HomeController")) ")
}
</script>
I put some alert("message") before .load() function, and it shows a message, but then I put it after .load(), it doesn't show it. I've tried with and without html.raw(). This is my controller action
我在 .load() 函数之前放了一些 alert("message"),它显示了一条消息,但是我把它放在 .load() 之后,它没有显示。我试过使用和不使用 html.raw()。这是我的控制器操作
public ActionResult doctorsview()
{
return PartialView("~/Views/_Doktor.cshtml", new Doktor());
}
Where am I wrong?
我哪里错了?
回答by Ehsan Sajjad
you don't have to pass complete name of HomeController, In Url.Action()we have to pass prefix of Controller name, when we create controller we have to add a postfix of Controllerin mvc and when we use it in helpers we have to just pass Controller name without Controllerpostfix:
你不必传递 的完整名称HomeController,Url.Action()我们必须传递控制器名称的前缀,当我们创建控制器时,我们必须Controller在 mvc 中添加一个后缀,当我们在帮助器中使用它时,我们只需要传递控制器名称而没有Controller后缀:
public class HomeController: Controller
{
public ActionResult doctorsview()
{
return PartialView("~/Views/_Doktor.cshtml", new Doktor());
}
}
Do like this:
这样做:
$("#someDiv").load('@Url.Action("doctorsview","Home")');
and you should be placing it in respective Views directory of Controller, which is in this case Views -> Home
并且您应该将其放置在 Controller 的相应 Views 目录中,在本例中为Views -> Home

