C# 如何为局部视图使用控制器?
我有下面的模型 我需要左上角的用户配置文件是动态加载的,所以我将navigation.cshtml分为两个文件 Navigation.cshtmlC# 如何为局部视图使用控制器?,c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Razor,我有下面的模型 我需要左上角的用户配置文件是动态加载的,所以我将navigation.cshtml分为两个文件 Navigation.cshtml <nav class="navbar-default navbar-static-side" role="navigation"> <div class="sidebar-collapse"> <ul class="nav" id="side-menu"> <
<nav class="navbar-default navbar-static-side" role="navigation">
<div class="sidebar-collapse">
<ul class="nav" id="side-menu">
<li class="nav-header">
<!-- Top Navbar -->
@Html.Partial("_Profile")
</li>
<li class="@Html.IsSelected(action: "Index")">
<a href="@Url.Action("Index", "Home")"><i class="fa fa-laptop"></i> <span class="nav-label">Main page</span> </a>
</li>
<li class="@Html.IsSelected(action: "Minor")">
<a href="@Url.Action("Minor", "Home")"><i class="fa fa-desktop"></i> <span class="nav-label">Minor page</span></a>
</li>
</ul>
</div>
</nav>
-
@Html.Partial(“_Profile”)
-
-
和profile.cshtml
<div class="dropdown profile-element">
<span>
<img alt="image" class="img-circle" src="~/Images/profile_small.jpg" />
</span>
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs">
<strong class="font-bold">David Williams</strong>
</span> <span class="text-muted text-xs block">Art Director <b class="caret"></b></span>
</span>
</a>
<ul class="dropdown-menu animated fadeInRight m-t-xs">
<li><a href="@Url.Action("Profile", "AppViews")">Profile</a></li>
<li><a href="@Url.Action("Contacts", "AppViews")">Contacts</a></li>
<li><a href="@Url.Action("Inbox", "Mailbox")">Mailbox</a></li>
<li class="divider"></li>
<li><a href="@Url.Action("Login", "Pages")">Logout</a></li>
</ul>
</div>
<div class="logo-element">
IN+
</div>
在+
我有一个控制器,它将返回用户的名称
public class UserProfileController : Controller
{
// GET: UserProfile
public async Task<ActionResult> GetPropertiesForUser()
{
Uri serviceRoot = new Uri(SettingsHelper.AzureAdGraphApiEndPoint);
var token = await AppToken.GetAppTokenAsync();
ActiveDirectoryClient adClient = new ActiveDirectoryClient(
serviceRoot,
async () => await AppToken.GetAppTokenAsync());
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
Microsoft.Azure.ActiveDirectory.GraphClient.Application app = (Microsoft.Azure.ActiveDirectory.GraphClient.Application)adClient.Applications.Where(
a => a.AppId == SettingsHelper.ClientId).ExecuteSingleAsync().Result;
if (app == null)
{
throw new ApplicationException("Unable to get a reference to application in Azure AD.");
}
string requestUrl = string.Format("https://graph.windows.net/{0}/users/{1}?api-version=1.5", SettingsHelper.Tenant,ClaimsPrincipal.Current.Identities.First().Name);
HttpClient hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
"Bearer", token);
HttpResponseMessage hrm = await hc.GetAsync(new Uri(requestUrl));
if (hrm.IsSuccessStatusCode)
{
string jsonresult = await hrm.Content.ReadAsStringAsync();
return View("GetPropertiesForUser", new UserProfile
{
DisplayName = jsonresult //I am still missing here to get the display name only
});
}
else
{
return View();
}
}
}
公共类UserProfileController:控制器
{
//获取:UserProfile
公共异步任务
公共类UserProfileController:控制器
{
公共异步任务
局部视图名称称为_Profile.cshtml,而不是使用Html。局部视图可以使用Html.Action或Html.RenderAction,指向控制器操作。类似于:
<div class="dropdown profile-element">
<span>
<img alt="image" class="img-circle" src="~/Images/profile_small.jpg" />
</span>
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs">
<strong class="font-bold">David Williams</strong>
</span> <span class="text-muted text-xs block">Art Director <b class="caret"></b></span>
</span>
</a>
<ul class="dropdown-menu animated fadeInRight m-t-xs">
<li><a href="@Url.Action("Profile", "AppViews")">Profile</a></li>
<li><a href="@Url.Action("Contacts", "AppViews")">Contacts</a></li>
<li><a href="@Url.Action("Inbox", "Mailbox")">Mailbox</a></li>
<li class="divider"></li>
<li><a href="@Url.Action("Login", "Pages")">Logout</a></li>
</ul>
</div>
<div class="logo-element">
IN+
</div>
<li class="nav-header">
<!-- Top Navbar -->
@Html.Action("GetPropertiesForUser")
您可以使用Html.Action或Html.RenderAction来代替Html.Partial,指向控制器操作。类似于:
<li class="nav-header">
<!-- Top Navbar -->
@Html.Action("GetPropertiesForUser")
实际上,您需要一个ChildAction和部分视图,因为您希望GetPropertiesForUser操作使用profile.cshtml视图
public class UserProfileController : Controller
{
[ChildActionOnly]
public ActionResult GetPropertiesForUser()
{
// ...
return PartialView(...);
}
}
用法
或
实际上,您需要一个ChildAction和部分视图,因为您希望GetPropertiesForUser操作使用profile.cshtml视图
public class UserProfileController : Controller
{
[ChildActionOnly]
public ActionResult GetPropertiesForUser()
{
// ...
return PartialView(...);
}
}
用法
或
我读到一些渲染操作会导致服务器出现2次错误,对吗?@EstebanV请提供一个链接-没有额外的往返来渲染子操作。注意-不清楚您是否读到了错误的链接,还是完全错误。这里是注释。我读到一些渲染操作会导致服务器出现2次错误,是吗正确吗?@EstebanV请提供一个链接-没有额外的往返来呈现子操作。注意-不清楚您是否读取了错误的链接或它只是完全错误。此处,在注释上。子操作不能是异步的…VNext支持异步视图组件,但语法不同。子操作不能是异步的…VNext support异步查看组件,但语法不同。
@{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); }