比较来自世界各地的卖家的域名和 IT 服务价格

获得当前 ViewContext 在 ASP.Net MVC

我有一个功能 ASP.Net MVC JsonResult, 其中我想返回内容 a PartialView /必须使用内容使用 Ajax, 而出于某种原因,我不能回来 a PartialViewResult/.

用于渲染 PartialView 我需要这个对象 ViewContext.

如何获得当前对象 ViewContext 在行动方法? 我甚至没有看到 HttpContext.Current 在其动作方法中。

我用 ASP.net MVC 1.
已邀请:

龙天

赞同来自:

a ViewContext 在方法中不可用 action, 由于它在渲染演示后稍后创建。 我建议你使用
http://www.brightmix.com/blog/ ... -mvc/
用于呈现字符串中部分呈现的内容。

风见雨下

赞同来自:

也许我在某处遗漏了一些时间,但我的行为返回局部视图,返回指的对象是指页面的演示文稿 ascx. 它将归还部分 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>&gt;" %&gt;
&lt;% foreach /var item in Model/ { %&gt;
<div class="principleTitle" title="&lt;%= Html.Encode/item.NaturalKey/ %&gt;">&lt;%= Html.Encode/item.Title/ %&gt;</div>
&lt;%} %&gt;


最后, 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'/;
}/;


javascript, 处理返回的数据 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//;
}
};


最后,返回数据的动作本身 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/;
}


它会有所帮助吗? 如果没有,您无法发布您工作的代码的一部分,因为我错过了一些东西。
</ienumerable<myproject.data.principle>

要回复问题请先登录注册