Asp.net mvc 在ASP.Net MVC中获取当前ViewContext
我有一个ASP.Net MVC JsonResult函数,在该函数中,我希望返回PartialView的内容(必须使用Ajax加载内容,由于某些原因,我无法返回PartialViewResult) 要渲染PartialView,我需要ViewContext对象 如何在操作方法中获取当前ViewContext对象?在我的操作方法中,我甚至看不到HttpContext.CurrentAsp.net mvc 在ASP.Net MVC中获取当前ViewContext,asp.net-mvc,jsonresult,viewcontext,Asp.net Mvc,Jsonresult,Viewcontext,我有一个ASP.Net MVC JsonResult函数,在该函数中,我希望返回PartialView的内容(必须使用Ajax加载内容,由于某些原因,我无法返回PartialViewResult) 要渲染PartialView,我需要ViewContext对象 如何在操作方法中获取当前ViewContext对象?在我的操作方法中,我甚至看不到HttpContext.Current 我正在使用ASP.net MVC 1。我可能在某些地方遗漏了一点,但我返回部分视图的操作是通过返回引用ascx页面的
我正在使用ASP.net MVC 1。我可能在某些地方遗漏了一点,但我返回部分视图的操作是通过返回引用ascx页面的视图对象来实现的。这将返回不包含完整页面结构(HTML、head、body等)的部分HTML。不确定您为什么要做除此之外的任何事情,是否有需要返回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):
所以,一个完整的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();
}
};
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);
}
最后是返回json数据的操作本身:
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();
}
};
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);
}
这有用吗?如果没有,那么您可以发布一些您正在处理的代码,因为我遗漏了一些内容。操作方法中没有ViewContext,因为它是在渲染视图之前构建的。我建议您使用将局部视图的内容渲染为字符串。这正是我所需要的。现在试着让它工作。谢谢,达林。我显然不知道需要这个,有没有人能给我举个例子,说明哪些地方需要这个?这对我来说更快、更容易。thriftybliss也是如此。Lazarus,我想返回一个JSONResult,因为我通过Ajax返回的数据有一些元素需要在客户端使用Javascript进行处理。我不想为JSon和Partial调用两个单独的函数,所以我想将Partial呈现为JSon val.JsonResult jsn=JSon(新字典{{“success”,true},{“lastPID”,posts[0].ID},{“content”,Globals.RenderPartialToString(“~/Views/Partial/posts.ascx”,ViewData,this.ControllerContext,TempData)});