我可以在.NET控制器上返回不同的剃刀视图吗?

时间:2018-10-24 17:20:41

标签: c# asp.net asp.net-mvc

背景是这个。我有一个控制器,用于显示故事对象的详细信息(您可以将其视为博客文章)。

我希望任何人都可以在不登录应用程序的情况下阅读故事的详细信息。但是,在我的StoryDetailsViewModel中,有一个ApplicationUser字段。之所以这样做,是因为如果有人想对这个故事发表评论,我需要知道作者是谁,因此,我想强迫他们登录以发表评论。

我将[AllowAnonymous]作为控制器操作的属性。当我尝试获取已登录用户的身份时,如果该人未登录,则调用返回null,将null插入viewmodel的ApplicationUser字段中,从而中断视图。我的控制器操作如下。

这整个过程是因为在视图中,如果有人登录,我需要这个文本区域:

enter image description here

我不知道我是否应该使用某种布尔值,如果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; }
    }

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可能不需要使用其他视图。

您可以将bool之类的IsAnonymous属性添加到StoryDetailsViewModelSignedInUser来指示用户是否登录,或者检查是否设置了属性User({ {1}})。最后,在您的视图中,使用这些属性显示或隐藏“注释”部分/部分视图。

ViewModel:

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);
}