[英]How to use a controller for a partial view?
我有以下樣機。
我需要動態加載左上角的用戶個人資料,因此我將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
實際上,您需要具有部分視圖的ChildAction ,因為您希望GetPropertiesForUser操作使用profile.cshtml視圖。
public class UserProfileController : Controller
{
[ChildActionOnly]
public ActionResult GetPropertiesForUser()
{
// ...
return PartialView(...);
}
}
@Html.Action("GetPropertiesForUser", "UserProfile")
要么
@{ Html.RenderAction("GetPropertiesForUser", "UserProfile"); }
代替使用Html.Partial,可以使用Html.Action或Html.RenderAction,指向控制器操作。 像這樣的東西:
<li class="nav-header">
<!-- Top Navbar -->
@Html.Action("GetPropertiesForUser")
確保從Controller而不是View()
返回PartialView()
View()
。
關於Update 1中的錯誤,您需要使用命名約定(Action和View之間的共享名)或指定視圖的名稱。
在您的情況下,我認為您需要將返回值替換為以下內容:
return PartialView("_Profile");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.