如何通过jQuery加载局部视图? [英] How to load partial view via jQuery?

查看:108
本文介绍了如何通过jQuery加载局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery加载部分视图。部分视图是从Contact.cshtml加载的。但是,在Chrome中,我在尝试加载partialViewName.cshtml时不断获得404.

I'm trying to load a partial view using jQuery. The partial view is being loaded from Contact.cshtml. However, in Chrome, I keep getting a 404 when trying to load partialViewName.cshtml.

我有以下文件夹结构:

/Views/Contact/Contact.cshtml

/Views/Contact/Contact.cshtml

/Views/Contact/partialViewName.cshtml

/Views/Contact/partialViewName.cshtml

$('#divname').load('partialViewName');

我所在的网址是 http:// localhost /联系/索引

任何想法我做错了什么?

Any ideas what I'm doing wrong?

推荐答案

文件夹结构和.cshtml文件的名称无关紧要。你应该对控制器动作进行ajax调用,返回你想要的局部视图。

The folder structure and the name of the .cshtml file is irrelevant. You should make an ajax call to controller action which returns the partial view you want.

返回局部视图的控制器动作方法如下所示:

The controller action method returning the partial view should look like following,

//
// GET: /SampleController/MyAction

[HttpGet]
public ActionResult MyAction()
{
   return PartialView("_MyPartial");
}

然后你需要调用这个方法,

Then you need to make a call to this method,

$('#divname').load("/SampleController/MyAction");

这篇关于如何通过jQuery加载局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆