获取 ASP.Net MVC 中的当前 ViewContext

发布于 2024-08-17 13:38:14 字数 264 浏览 3 评论 0原文

我有一个 ASP.Net MVC JsonResult 函数,我想在其中返回 PartialView 的内容(该内容必须使用 Ajax 加载,并且由于某种原因我无法返回 PartialViewResult)。

为了渲染 PartialView 我需要 ViewContext 对象。

如何在 Action 方法中获取当前 ViewContext 对象?我什至在我的操作方法中没有看到 HttpContext.Current。

我正在使用 ASP.net MVC 1。

I have a ASP.Net MVC JsonResult function in which I want to return the contents of a PartialView (The content has to be loaded using Ajax, and for some reason I can't return a PartialViewResult).

To render the PartialView I need the ViewContext object.

How do you get the current ViewContext object within an Action method? I don't even see HttpContext.Current in my action method.

I am using ASP.net MVC 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩发小公举 2024-08-24 13:38:15

我可能在某个地方错过了一点,但我返回部分视图的操作是通过返回引用 ascx 页面的 View 对象来实现的。这将返回部分 HTML,而不包含完整的页面结构(html、head、body 等)。不确定为什么您想要做除此之外的任何事情,您需要返回 PartialViewResult 是否有特定原因?这是我的工作代码中的一个示例。

首先是我的控制器中的操作:

    public ViewResult GetPrincipleList(string id)
    {
        if (id.Length > 1)
            id = id.Substring(0, 1);
        var Principles = competitorRepository.Principles.Where(p => p.NaturalKey.StartsWith(id)).Select(p=>p);
        return View(Principles);
    }

然后是部分视图 (ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyProject.Data.Principle>>" %>
    <% foreach (var item in Model) { %>
 <div class="principleTitle" title="<%= Html.Encode(item.NaturalKey) %>"><%= Html.Encode(item.Title) %></div>
<%} %>

最后是设置调用的 Jquery:

    $(function() {
        $(".letterSelector").click(function() {
            $("#principleList").load("/GetPrincipleList/" + $(this).attr("title"), null, setListClicks);
        });
    });

所以,一个完整的 AJAX 过程,希望有所帮助。

---- 更新以下评论 ----

返回 Json 数据也同样简单:

首先,当选择框更改时启动 AJAX 调用:

    $("#users").change(function() {
        var url = "/Series/GetUserInfo/" + $("#users option:selected").attr("value");
        $.post(url, null, function(data) { UpdateDisplay(data); }, 'json');
    });

处理返回的 json 数据的 javascript:

function UpdateDisplay(data) {
    if (data != null) {
        $("div.Message").fadeOut("slow", function() { $("div.Message").remove(); });
        $("#Firstname").val(data.Firstname);
        $("#Lastname").val(data.Lastname);
        $("#List").val(data.List);
        $("#Biography").val(data.Biography);
        if (data.ImageID == null) {
            $("#Photo").attr({ src: "/Content/Images/nophoto.png" });
            $("#ImageID").val("");
        }
        else {
            if (data.Image.OnDisk) {
                $("#Photo").attr({ src: data.Image.ImagePath });
            }
            else {
                $("#Photo").attr({ src: "/Series/GetImage?ImageID=" + data.ImageID });
            }
            $("#ImageID").val(data.ImageID);
        }
        $("form[action*='UpdateUser']").show();
    } else {
        $("form[action*='UpdateUser']").hide();
    }
};

最后是返回 json 数据的 Action 本身:

    public JsonResult GetUserInfo(Guid id)
    {
        MyUser myuser = (from u in seriesRepository.Users
                       where u.LoginID == id
                       select u).FirstOrDefault();
        if (myuser  == null)
        {
            myuser = new MyUser();
            myuser.UserID = 0;
            myuser.Firstname = Membership.GetUser(id).UserName;
            myuser.Lastname = "";
            myuser.List = "";
            myuser.Biography = "No yet completed";
            myuser.LoginID = id;
        }
        return Json(myuser);
    }

有帮助吗?如果没有,那么您可以发布一些您正在处理的代码,因为我遗漏了一些东西。

I may have missed a point somewhere but my Actions that returned partial views do so by returning a View object that refers to an ascx page. This will return partial HTML without the full page constructs (html, head, body, etc.). Not sure why you'd want to do anything beyond that, is there a specific reason you need to return PartialViewResult? Here's an example from my working code.

First the Action in my controller:

    public ViewResult GetPrincipleList(string id)
    {
        if (id.Length > 1)
            id = id.Substring(0, 1);
        var Principles = competitorRepository.Principles.Where(p => p.NaturalKey.StartsWith(id)).Select(p=>p);
        return View(Principles);
    }

And then the partial view (ascx):

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<IEnumerable<MyProject.Data.Principle>>" %>
    <% foreach (var item in Model) { %>
 <div class="principleTitle" title="<%= Html.Encode(item.NaturalKey) %>"><%= Html.Encode(item.Title) %></div>
<%} %>

Lastly, the Jquery that sets up the call:

    $(function() {
        $(".letterSelector").click(function() {
            $("#principleList").load("/GetPrincipleList/" + $(this).attr("title"), null, setListClicks);
        });
    });

So, a full AJAX process, hope that helps.

---- UPDATE following comment ----

Returning Json data is just as easy:

Firstly, initiating the AJAX call when a select box changes:

    $("#users").change(function() {
        var url = "/Series/GetUserInfo/" + $("#users option:selected").attr("value");
        $.post(url, null, function(data) { UpdateDisplay(data); }, 'json');
    });

The javascript that processes the returned json data:

function UpdateDisplay(data) {
    if (data != null) {
        $("div.Message").fadeOut("slow", function() { $("div.Message").remove(); });
        $("#Firstname").val(data.Firstname);
        $("#Lastname").val(data.Lastname);
        $("#List").val(data.List);
        $("#Biography").val(data.Biography);
        if (data.ImageID == null) {
            $("#Photo").attr({ src: "/Content/Images/nophoto.png" });
            $("#ImageID").val("");
        }
        else {
            if (data.Image.OnDisk) {
                $("#Photo").attr({ src: data.Image.ImagePath });
            }
            else {
                $("#Photo").attr({ src: "/Series/GetImage?ImageID=" + data.ImageID });
            }
            $("#ImageID").val(data.ImageID);
        }
        $("form[action*='UpdateUser']").show();
    } else {
        $("form[action*='UpdateUser']").hide();
    }
};

And finally the Action itself that returns the json data:

    public JsonResult GetUserInfo(Guid id)
    {
        MyUser myuser = (from u in seriesRepository.Users
                       where u.LoginID == id
                       select u).FirstOrDefault();
        if (myuser  == null)
        {
            myuser = new MyUser();
            myuser.UserID = 0;
            myuser.Firstname = Membership.GetUser(id).UserName;
            myuser.Lastname = "";
            myuser.List = "";
            myuser.Biography = "No yet completed";
            myuser.LoginID = id;
        }
        return Json(myuser);
    }

Does that help? If not then can you post some of the code you are working on as I'm missing something.

往日 2024-08-24 13:38:14

ViewContext 在操作方法中不可用,因为它是在渲染视图之前构建的。我建议您使用 MVCContrib 的 BlockRenderer 将部分视图的内容呈现为字符串。

a ViewContext is not available within the action method because it is constructed later before rendering the view. I would suggest you using MVCContrib's BlockRenderer to render the contents of a partial view into a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文