首页 > 解决方案 > 从 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?

标签: c#ajaxasp.net-mvc

解决方案


您可以将它放在一个元素中并查询它,例如在您的部分中:

<input type="hidden" id="isStudentHidden" value="@Model.IsStudent" />

然后在你完成的处理程序中:

var isStudent = $("body").find("#isStudentHidden").val();

推荐阅读