我有以下模型。
我需要左上角的用户个人资料是dinamically加载的,所以我将navigation.cshtml拆分为两个文件。
Navigation.cshtml
<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>
和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();
}
}
}
我不知道该怎么做
如何使用具有局部视图的控制器?
更新1。
我根据以下用户的建议更新了代码:
<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.RenderAction("GetPropertiesForUser", "UserProfile"); }
</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>
public class UserProfileController : Controller
{
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)
{
UserProfile up = JsonConvert.DeserializeObject<UserProfile>(await hrm.Content.ReadAsStringAsync());
return PartialView(up);
//return View("GetPropertiesForUser", new UserProfile
//{
// DisplayName = up.displayName() //I am still missing here to get the display name only
//});
}
else
{
return View();
}
}
}
@model Inspinia_MVC5_Capatech.Models.UserProfile
<div class="dropdown profile-element">
<span>
<img a...
但是我收到此错误: http://screencast.com/t/8VKj4Hwg
部分视图名称称为_Profile.cshtml
答案 0 :(得分:4)
实际上,您需要 ChildAction 部分视图,因为您希望 GetPropertiesForUser 操作使用 profile.cshtml 视图
public class UserProfileController : Controller
{
[ChildActionOnly]
public ActionResult GetPropertiesForUser()
{
// ...
return PartialView(...);
}
}
@Html.Action("GetPropertiesForUser", "UserProfile")
OR
@{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); }
答案 1 :(得分:2)
您可以使用Html.Action或Html.RenderAction代替控制器操作,而不是使用Html.Partial。有点像:
<li class="nav-header">
<!-- Top Navbar -->
@Html.Action("GetPropertiesForUser")
确保从Controller返回PartialView()
而不是View()
。
关于 Update 1 中的错误,您需要使用命名约定(Action和View之间的共享名称)或指明视图的名称。
在你的情况下,我认为你需要将回报替换为:
return PartialView("_Profile");