// 正确的Ajax请求示例
$.ajax({
url: '/Controller/Action', // 确保路径正确
type: 'GET', // 或'POST',与控制器方法匹配
data: { param1: value1 }, // 可选参数
success: function(result) {
$('#targetElement').html(result); // 确保选择器正确
},
error: function(xhr, status, error) {
console.log("Error: " + error); // 查看控制台错误
}
});
// 确保控制器方法返回PartialView
public ActionResult YourAction()
{
// 从SQL Server获取数据
var model = dbContext.YourModel.ToList();
return PartialView("_YourPartialView", model);
}
确保部分视图(_YourPartialView.cshtml
)位于正确的视图文件夹中,并且有正确的模型声明:
@model IEnumerable<YourNamespace.YourModel>
@foreach(var item in Model)
{
<div>@item.PropertyName</div>
}
确保Web.config
中的连接字符串正确:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=yourServer;Initial Catalog=yourDB;User ID=user;Password=password;"
providerName="System.Data.SqlClient" />
</connectionStrings>
通过以上步骤的检查和修正,应该能够解决Ajax不检索部分视图数据的问题。
没有搜到相关的沙龙