Skip to main content
 首页 » 编程设计

c#之Ajax 发布不工作 MVC 5

2024年02月05日35www_RR

我正在尝试将数据发布到 Controller 中,但它似乎不起作用,我需要在完成后将 View 的内容包含到 div 中,但我无法完全实现它

这是我的 js 函数:

function show(num) { 
    $.ajax({ 
        dataType: "html", 
        type: "POST", 
        url: "Student/Schedule", 
        data: { number: num }, 
        success: function (a) { 
            // Replace the div's content with the page method's return. 
            alert("success"); 
            $('#schedule').load(a); 
        } 
    }); 
} 

而且,这是我的 Controller :

public ActionResult Schedule(String number) 
{ 
    return View(number); 
} 

我是 MVC 和 C# 的菜鸟,所以欢迎任何帮助。

请您参考如下方法:

有些事情您应该解决才能解决问题。

  • 将 Url 更改为 "/Student/Schedule"

    您正在使用 "Student/Schedule" 作为 url,因此您正在尝试调用名为“Student”的操作。

  • 将 [HttpPost] 添加到您的操作中。

  • 您应该返回 PartialView("Schedule", number);。

    当您使用 return View(number) 时,它使用数字的字符串值作为您的 View 名称。您应该显式传递 View 名称和模型。

  • 使用$('#schedule').html(a);

最好在 ajax 调用中添加一个错误函数以便能够发现错误:

error: function (jqXHR, textStatus, errorThrown) {  
    alert(errorThrown);  
    //or you can put jqXHR.responseText somewhere as complete response. Its html. 
}