[英]Can I return different razor views on .NET controller?
背景是這樣的。 我有一個控制器,用於顯示故事對象的詳細信息(您可以將其視為博客文章)。
我希望任何人都可以在不登錄應用程序的情況下閱讀故事的詳細信息。 但是,在我的StoryDetailsViewModel
我有一個ApplicationUser
字段。 之所以這樣做,是因為如果有人想對這個故事發表評論,我需要知道作者是誰,因此,我想強迫他們登錄以發表評論。
我的控制器操作具有[AllowAnonymous]
屬性。 當我嘗試獲取登錄用戶的身份時,如果該人未登錄,則調用將返回null,將null插入viewmodel的ApplicationUser
字段中,從而中斷視圖。 我的控制器動作如下。
這整個過程是因為在視圖中,如果有人登錄,我需要這個文本區域:
我不知道是否應該有某種布爾值,如果User.Identity.GetUserId()
返回null,我可以對此進行操作,或者按照下面的控制器操作,嘗試根據是否創建兩個單獨的視圖模型用戶是匿名的或已登錄。
對解決此問題的最佳方式(最有效方式)有什么想法?
[HttpGet]
[AllowAnonymous]
public ActionResult Details(int id)
{
var FoundStory = _dbContext.Stories.SingleOrDefault(x => x.Id == id);
if (FoundStory == null)
{
return HttpNotFound();
}
//get the logged in userId
string signedInUserId = User.Identity.GetUserId();
//if the person reading the article isn't signed in, the userId will come back null
//need to create a viewmodel and view that doesn't have a signed in user associated with it
if (signedInUserId == null)
{
var viewModel = new StoryDetailsViewModelAnonymousUser
{
StoryId = FoundStory.Id,
AuthorId = FoundStory.AuthorId,
Story = FoundStory,
Comments = _dbContext.Comments.Where(x => x.StoryId == FoundStory.Id).ToList()
};
return View(viewModel);
} else
{
var viewModel = new StoryDetailsViewModelSignedInUser
{
StoryId = FoundStory.Id,
AuthorId = FoundStory.AuthorId,
Story = FoundStory,
User = _dbContext.Users.SingleOrDefault(x => x.Id == signedInUserId),
Comments = _dbContext.Comments.Where(x => x.StoryId == FoundStory.Id).ToList()
};
return View(viewModel);
}
}
我的viewModel:
public class StoryDetailsViewModelSignedInUser
{
public ApplicationUser User { get; set; }
public int StoryId { get; set; }
public Story Story { get; set; }
public string AuthorId { get; set; }
[Required]
public string Content { get; set; }
public IEnumerable<Comment> Comments { get; set; }
}
在這種情況下,您可能不需要使用其他視圖。
您可以添加一個bool
屬性,如IsAnonymous
到StoryDetailsViewModelSignedInUser
指示,如果用戶登錄或沒有,或檢查屬性User
設置( model.User != null
)。 最后,在您的視圖中,使用這些屬性顯示或隱藏“注釋”部分/部分視圖。
視圖模型:
public class StoryDetailsViewModelSignedInUser
{
public bool IsAnonymous
{
get
{
return User != null;
}
}
public ApplicationUser User { get; set; }
// other properties here
}
鑒於您所要做的就是添加一條if
語句來檢查用戶是否登錄,因此對注釋部分使用局部視圖可能會使您的生活更輕松。
我還將重構控制器方法,以減少代碼重復。 代替:
if (signedInUserId == null)
{
var viewModel = new StoryDetailsViewModelAnonymousUser
{
StoryId = FoundStory.Id,
AuthorId = FoundStory.AuthorId,
Story = FoundStory,
Comments = _dbContext.Comments.Where(x => x.StoryId == FoundStory.Id).ToList()
};
return View(viewModel);
} else
{
var viewModel = new StoryDetailsViewModelSignedInUser
{
StoryId = FoundStory.Id,
AuthorId = FoundStory.AuthorId,
Story = FoundStory,
User = _dbContext.Users.SingleOrDefault(x => x.Id == signedInUserId),
Comments = _dbContext.Comments.Where(x => x.StoryId == FoundStory.Id).ToList()
};
return View(viewModel);
}
你可以做:
var viewModel = new StoryDetailsViewModelAnonymousUser
{
StoryId = FoundStory.Id,
AuthorId = FoundStory.AuthorId,
Story = FoundStory,
Comments = _dbContext.Comments.Where(x => x.StoryId == FoundStory.Id).ToList()
};
if (signedInUserId != null)
{
viewModel.User = _dbContext.Users.SingleOrDefault(x => x.Id == signedInUserId);
}
return View(viewModel);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.