我有一個ASP.Net MVC JsonResult函數,我想返回一個PartialView的內容(內容必須使用Ajax加載,出於某種原因,我不能返回一個PartialViewResult)。獲取當前ViewContext在ASP.Net MVC
要呈現PartialView,我需要ViewContext對象。
如何獲取Action方法中的當前ViewContext對象?我甚至在我的操作方法中看不到HttpContext.Current。
我使用ASP.net MVC 1
我有一個ASP.Net MVC JsonResult函數,我想返回一個PartialView的內容(內容必須使用Ajax加載,出於某種原因,我不能返回一個PartialViewResult)。獲取當前ViewContext在ASP.Net MVC
要呈現PartialView,我需要ViewContext對象。
如何獲取Action方法中的當前ViewContext對象?我甚至在我的操作方法中看不到HttpContext.Current。
我使用ASP.net MVC 1
ViewContext在操作方法中不可用,因爲它在渲染視圖之前稍後構建。我建議您使用MVCContrib's BlockRenderer將部分視圖的內容呈現爲字符串。
我可能已經錯過了一個點的地方,但我的行爲是返回局部視圖通過返回指ASCX頁面視圖對象。這將返回部分HTML沒有完整的頁面結構(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):
<%@ 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過程,希望有所幫助。
---- UPDATE如下評論----
返回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();
}
};
最後是A本身返回json數據:
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);
}
這有幫助嗎?如果沒有,那麼你可以發佈你正在處理的一些代碼,因爲我錯過了一些東西。
Lazarus,我想返回一個JSONResult,因爲我通過Ajax返回的數據有一些元素需要在客戶端使用Javascript進行處理。我不想爲JSon和Partial調用兩個單獨的函數,所以我想將部分渲染爲json val。 – 2010-01-07 08:42:36
JsonResult jsn = Json(new Dictionary
啊!我明白你爲什麼需要這個。 – Lazarus 2010-01-07 13:29:19
這正是我所需要的。現在試圖去實現它的工作。謝謝Darin。 – 2010-01-07 08:41:33
我很明顯錯過了這個需求,任何人都會友好地給我一個例子,這將是必要的嗎? – Lazarus 2010-01-07 09:07:19
http://thriftybliss.spaces.live.com/blog/cns!58DA805F37F31F20!170.entry?wa=wsignin1.0&sa=362921628雖然這對我來說更快更簡單。 – 2010-01-08 06:24:41