Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 如何在ASP.NET MVC(核心)中为视图模型中的链接传递数据_Asp.net Mvc_Asp.net Core_Asp.net Mvc Viewmodel - Fatal编程技术网

Asp.net mvc 如何在ASP.NET MVC(核心)中为视图模型中的链接传递数据

Asp.net mvc 如何在ASP.NET MVC(核心)中为视图模型中的链接传递数据,asp.net-mvc,asp.net-core,asp.net-mvc-viewmodel,Asp.net Mvc,Asp.net Core,Asp.net Mvc Viewmodel,让我们假设我有一个博客文章的视图,我需要为该视图提供一个视图模型。视图将显示 作者姓名 日期 评论数 标签 如果这些只是文本元素,我会选择 public PostViewModel { public string AuthorName { get; set; } public DateTime Date { get; set; } public int NumberOfComments { get; set; } public IEnumerable<strin

让我们假设我有一个博客文章的视图,我需要为该视图提供一个视图模型。视图将显示

  • 作者姓名
  • 日期
  • 评论数
  • 标签
如果这些只是文本元素,我会选择

public PostViewModel
{
   public string AuthorName { get; set; }
   public DateTime Date { get; set; }
   public int NumberOfComments { get; set; }
   public IEnumerable<string> Tags { get; set; }
}
PostViewModel
成为

public PostViewModel
{
   public ActionViewModel AuthorName { get; set; }
   public DateTime Date { get; set; }
   public ActionViewModel NumberOfComments { get; set; }
   public IEnumerable<ActionViewModel> Tags { get; set; }
}

因为标记名和它的ID之间没有安全的关联,所以我必须在这里引入一个对象。

您可以简单地为post ID添加一个属性,为author ID添加另一个属性并使用它们

public PostViewModel
{
   public int Id {set;get;}
   public int AuthorId { set;get;
   public string AuthorName { get; set; }
   public DateTime Date { get; set; }
   public int NumberOfComments { get; set; }
}
在你看来

@model PostViewModel
<p>Post created by @Html.ActionLink(Model.AuthorName,"Details","User"
                                                        ,new { id=Model.AuthorId},null)</p>
<p>Total @Html.ActionLink(Model.NumberOfComments,"Comments","Post"
                                                   ,new { postId =Model.AuthorId},null)</p>

现在在您看来,您应该访问作者Id/名称,如
Model.author.Id

,这将是我问题中的选项2。问题是,我可以通过引入集合(标记)等方式轻松地将其弄得更加混乱。视图模型应该具有特定视图所需的属性。因此,如果视图需要显示标记,则需要将该标记的属性添加到视图模型中。这并没有什么错,我实际上喜欢你的ActionViewModel,因为它以一种灵活的方式允许动态链接,所以可以重用
public PostViewModel
{
   public int Id {set;get;}
   public int AuthorId { set;get;
   public string AuthorName { get; set; }
   public DateTime Date { get; set; }
   public int NumberOfComments { get; set; }
}
@model PostViewModel
<p>Post created by @Html.ActionLink(Model.AuthorName,"Details","User"
                                                        ,new { id=Model.AuthorId},null)</p>
<p>Total @Html.ActionLink(Model.NumberOfComments,"Comments","Post"
                                                   ,new { postId =Model.AuthorId},null)</p>
public class BaseVm
{
  public int Id {set;get;}
  public string Name { set;get;}
}
public class AuthorVm : BaseVm
{
}
public class TagVm : BaseVm
{
}
public PostViewModel
{
   public int Id {set;get;}
   public AuthorVm Author { set;get;  
   public DateTime Date { get; set; }
   public int NumberOfComments { get; set; }
   public List<TagVm> Tags {set;get;}

   public PostViewModel()
   {
     this.Tags = new List<TagVm>();
     this.Author = new AuthorVm();
   }
}