C# 如何在视图中使用两个模型
如何在同一视图中使用两个不同的模型 我为什么需要这个? 我需要创建一个客户机填充注册表的视图,我需要一个组合框/选择表单(这个表单需要从另一个模型加载字符串) 以下是我尝试过的: 为两个模型创建ViewModel,如下所示:C# 如何在视图中使用两个模型,c#,asp.net,asp.net-core,C#,Asp.net,Asp.net Core,如何在同一视图中使用两个不同的模型 我为什么需要这个? 我需要创建一个客户机填充注册表的视图,我需要一个组合框/选择表单(这个表单需要从另一个模型加载字符串) 以下是我尝试过的: 为两个模型创建ViewModel,如下所示: public class CandidateViewModel { public int Id { get; set; } public string Name { get; set; } public int Number { get; s
public class CandidateViewModel
{
public int Id { get; set; }
public string Name { get; set; }
public int Number { get; set; }
public string Profile { get; set; }
public Byte[] CV { get; set; }
public string CVNAME { get; set; }
public List<Profile> ProfileList { get; set; }
public string ProfileText { get; set; }
}
但我不知道在这之后我到底需要做什么,我对这件事很陌生,我正在做这项工作,因为我还在学习,如果对我的问题有任何疑问,请问我,我会尽力解释
这是我的观点,我需要使用这两种模型。
@*model HCCBPOHR.Data.Candidate*@
@model HCCBPOHR.DomainModel.CandidateModel
@{
ViewData["Title"] = "CandidateCreate";
}
<h2>CandidateCreate</h2>
<h4>Candidate</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form method="post" enctype="multipart/form-data" asp-action="CandidateCreate">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Number" class="control-label"></label>
<input asp-for="Number" class="form-control" maxlength="9" />
<span asp-validation-for="Number" class="text-danger"></span>
</div>
<div class="form-group">
<label>Selects</label>
<select asp-for="Profile" class=" form-control ">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
</div>
<div class="form-group">
<label asp-for="CV" type="file" class="control-label"></label>
<input asp-for="CV" type="file" class="form-control" />
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-default" onclick="this.disabled=true;this.form.submit();" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@*型号HCCBPOHR.Data.Candidate*@
@模型HCCBPOHR.DomainModel.CandidateModel
@{
ViewData[“Title”]=“CandidateCreate”;
}
候选人创造
候选人
挑选
1.
2.
3.
4.
5.
返回列表
您可以在此场景中应用MVVM模式
下面是一个例子:
我将在模型文件夹中定义3个类
public class Post
{
public string Id {get;set;}
public string Content {get;set;}
public string UserId {get;set;}
}
public class Comment
{
public string Id {get;set;}
public string Content {get;set;}
public string PostId {get;set;}
}
// this will hold data for 2 class above
public class PostVM
{
public Post Post {get;set}
public Comment Comment {get;set}
}
然后,在我的控制器中,我将查询db以获取post和评论的数据,如下所示
public IActionResult PostDetail(string postId)
{
var post = _postRepository.GetPostById(postId);
var comment = _commentRepository.GetCommentByPostId(postId);
// MVVM model return to the view
var vM = new PostVM
{
Post = post,
Comment = comment
}
}
最后在我看来
@model PostVM
<div> @model.Post.Content </div>
@foreach(var comment in @model.Comment)
{
<div> @comment.Content</div>
}
@model PostVM
@模型后内容
@foreach(var comment in@model.comment)
{
@评论.内容
}
请根据您的代码进行相应调整。如果你有任何问题,请告诉我
干杯您可以在此场景中应用MVVM模式 下面是一个例子: 我将在模型文件夹中定义3个类
public class Post
{
public string Id {get;set;}
public string Content {get;set;}
public string UserId {get;set;}
}
public class Comment
{
public string Id {get;set;}
public string Content {get;set;}
public string PostId {get;set;}
}
// this will hold data for 2 class above
public class PostVM
{
public Post Post {get;set}
public Comment Comment {get;set}
}
然后,在我的控制器中,我将查询db以获取post和评论的数据,如下所示
public IActionResult PostDetail(string postId)
{
var post = _postRepository.GetPostById(postId);
var comment = _commentRepository.GetCommentByPostId(postId);
// MVVM model return to the view
var vM = new PostVM
{
Post = post,
Comment = comment
}
}
最后在我看来
@model PostVM
<div> @model.Post.Content </div>
@foreach(var comment in @model.Comment)
{
<div> @comment.Content</div>
}
@model PostVM
@模型后内容
@foreach(var comment in@model.comment)
{
@评论.内容
}
请根据您的代码进行相应调整。如果你有任何问题,请告诉我
干杯如果这个答案对您有帮助,请标记它,以便每个人都可以将这个答案作为参考。我在将它调整到我的代码时遇到问题。。。既然我展示了我的控制器和模型,还有我的视图,你能帮我吗?你现在有什么错误?请截图你的屏幕这不是一个代码问题,这是一个逻辑问题我想不出如何在我的代码上使用它如果这个答案帮助你,请标记它,这样每个人都可以把这个答案作为参考我有问题适应我的代码。。。既然我展示了我的控制器和模型,还有我的视图,你能帮我吗?你现在有什么错误?请截图你的屏幕这不是一个代码问题,这是一个逻辑问题我想不出如何在我的代码上使用它