为什么使用ajax找不到我的部分视图?

时间:2013-12-04 11:45:17

标签: ajax asp.net-mvc model-view-controller razor

我一直在关注如何使用ajax在现有网页中加载局部视图的书中的示例,但无法使其正常工作。

Chrome的元素检查员告诉我无法找到该文件(404),但是当我导航到该页面时,它会正常加载。

这是我的控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
   {
    //
    // GET: /Home/

    public ActionResult Index()
    {
        ViewBag.Message = "Hello";
        return View();
    }

    [HttpGet]
    public PartialViewResult HelloWorld()
    {
        ViewBag.Message = "Hello World";
        return PartialView();
    }

    }
}

以下是我的主要观点:

@{
Layout = null;
}

<!DOCTYPE html>
<html>
<head>
<title>index></title>
<script src="../../Scripts/jquery-1.8.3.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
</head>
<body>
<div>
    <div id="divMessage">@ViewBag.Message</div>
    @Ajax.ActionLink("Refresh", "HelloWorld", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "divMessage", InsertionMode = InsertionMode.Replace })
</div>
</body>
</html>

这是我的部分观点:

@ViewBag.Message

有人可以提出任何不起作用的原因吗?

由于

2 个答案:

答案 0 :(得分:2)

您将Ajax调用指定为POST,而PartialView方法为[HttpGet] 将ajax调用更改为“GET”或将方法更改为[HttpPost]

答案 1 :(得分:1)

您必须更改方法

public PartialViewResult HelloWorld()
 {
    ViewBag.Message = "Hello World";
    return PartialView();
 }

[HttpPost],因为你的ajax通话发了一个电话。

或者更改您的ajax通话以进行GET通话。