asp.net 如何从Controller JsonResult Action方法重新加载页面

wyyhbhjk  于 2023-02-14  发布在  .NET
关注(0)|答案(1)|浏览(102)

正在寻找从JsonResult Action方法重新加载/刷新视图页面的方法。我需要从其他条件重新加载它。下面的方法不起作用,我的意思是它不刷新StudentsData视图。* 下面的上载方法是一个GoogleDriveupload方法,上载并显示上载的文件。*

[HttpPost]
    public JsonResult Upload(string folderName, string contextType = null)
    {
        var currentAuth = Context.UserContextCache;
        var resultList = new List<ViewDataUploadFilesResult>();
        var CurrentContext = HttpContext;
        filesHelper.UploadAndShowResults(CurrentContext, resultList, folderName);
        JsonFiles files = new JsonFiles(resultList);


        if (!resultList.Any())
        {
            return Json("Error");
        }
        else
        {

            currentAuth.UserProfile.IITransferIntakeRequestMade = false;
            Context.UserContextCache = currentAuth;
            GetService<UserProfileService>().Save(Context.UserContextCache.UserProfile);

            //RELOAD SHOULD BE HERE
            RedirectToAction("StudentsData", "Document");

            return Json(files);

        }

    }
6pp0gazn

6pp0gazn1#

MVC中以JSON形式返回局部视图

public static class RazorViewToString
{
    public static string RenderRazorViewToString(Controller controller, string viewName, object model)
    {
        controller.ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);
            viewResult.ViewEngine.ReleaseView(controller.ControllerContext, viewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }
}

现在,您可以通过将“this”作为参数传递给Controller,在控制器文件中添加NameSpace,从而从控制器调用此类。

string result = RenderRazorViewToString(this, "ViewName", model);

相关问题