Ajax函数仅适用于MVC 3中的索引页

时间:2012-11-30 16:30:18

标签: ajax asp.net-mvc asp.net-mvc-3 jquery

我正在开发一个用ASP.Net MVC 3构建的项目。

在索引页面中,我显示了数据并允许用户使用jeditable(也使用ajax函数)和删除功能进行内联编辑。

在创建页面中,显然它允许用户输入新数据,但现在我需要添加一个功能,以便在数据库中已存在数据时允许从创建页面本身删除。

enter image description here 如果数据已存在(在我的情况下,如果扩展名不可用),则会在Extension的文本框模糊处显示确认框。

我正在使用ajax功能来实现这个功能但是没有工作,花了几个小时后,我能够找出导致问题的原因。

ajax功能仅适用于Index页面,而不适用于其他页面。我将创建页面重命名为索引,将原始索引重命名为其他内容,这次它工作正常,但是ajax停止在原始索引页面上工作,该页面已重命名为其他内容。 在URL中如果我将索引页面加载为“http://localhost:1234/Controller/Index”,页面加载正常但ajax函数不起作用。

以一种简单的方式: Ajax函数适用于

  • 本地主机:1234 /控制器

Ajax函数不适用于

  • 本地主机:1234 /控制器/创建
  • 本地主机:1234 /控制器/ ViewPage1
  • localhost:1234 / Controller / Index

如果有人可以解释为什么它的表现如此,那么为此解决这个问题会很好,如果可以修复的话,可以选择其他解决方案。

由于

Javascript代码

$('#Extension').blur(function () {

$.post("CheckPeople/checkDelete",
    {
        Extension: this.value
    },
    function (data) {
        alert(data);
    });
});

控制器:VB代码

        //GET: /People/

        Function Index() As ViewResult
            ViewBag.CurrentPage = "People"
            Return View(db.Peoples.ToList())
        End Function


        //GET: /People/Create

        Function Create() As ViewResult
            Return View()
        End Function


        //POST: /People/Create

        <HttpPost()>
        Function Create(ByVal people As People) As ActionResult
            If ModelState.IsValid Then
                db.Peoples.Add(people)
                db.SaveChanges()
                Return RedirectToAction("Index")
            End If

            Return View(people)
        End Function

        Public Function checkDelete(ByVal Extension As String) As JsonResult                
             Dim result As String = "I got you "
             Return Json(result, JsonRequestBehavior.AllowGet)
        End Function

感谢您的评论,我直接添加了代码。

如果我将Create.vbhtml重命名为Index.vbhtml并注释原始索引路由并重命名两个帖子并获得创建路由到索引,则ajax工作正常。

1 个答案:

答案 0 :(得分:2)

我认为这是一个相对网址问题。

尝试在ajax请求网址前添加/,例如

$.post("/CheckPeople/checkDelete",
    {
        Extension: this.value
    },
    function (data) {
        alert(data);
    });
});

希望这会有所帮助。