c# - 从 AJAX 返回的部分视图是否可以访问模型参数?
问题描述
所以我有一个用于刷新屏幕的 AJAX 方法。我将参数发送到控制器并返回部分视图。返回局部视图时,有什么方法可以访问模型参数?
AJAX 代码:
$(document).on("click", "#btnSave", function (e) {
var studentId = $('#StudentId').val();
$.ajax({
url: "/Student/InsertNew/",
type: 'POST',
data: {
studentId : studentId
},
cache: true,
async: true,
}).done(function (result) {
$("#divStudentTable").html(result);
if(result.IsStudent){
//How do I get the studentId value returned from the controller?
}
}
})
控制器 :
public ActionResult InsertNew(string studentId )
{
.
.
.
.
.
return PartialView("_StudentListPartial", model);
}
模型 :
public class RegisterRegistrantViewModel
{
public string StudentId { get; set; }
public bool IsStudent { get; set; }
}
那么有没有一种方法可以让我从 AJAX 成功中访问 IsStudent?
解决方案
您可以将它放在一个元素中并查询它,例如在您的部分中:
<input type="hidden" id="isStudentHidden" value="@Model.IsStudent" />
然后在你完成的处理程序中:
var isStudent = $("body").find("#isStudentHidden").val();
推荐阅读
- c++ - 移动窗口库
- javascript - Flask API 上的 json POST 不接受我的论点
- azure-logic-apps - 事件网格 - 逻辑应用程序 - 无法检索值
- asp.net - 使用 .net 和 vb,我如何处理会话和动态变量?
- linear-algebra - 并行求解多个小型线性系统
- database - 侦听数据库更新的工具
- xml - 动态 XSLT - 从网页而不是命令行运行转换
- c# - 单击时文本框的 Visual Studio 2015 C# 颜色对话框
- c# - 在 Mac 上运行的 .NET Core 上出现 X509Chain 错误
- django - 使用 MPTT 在 Post 之后将多个 ModelMutipleChoiceField 的值复制为一个