本文翻译自:Multiple models in a view
I want to have 2 models in one view. 我想在一个视图中有2个模型。 The page contains both LoginViewModel
and RegisterViewModel
. 该页面包含LoginViewModel
和RegisterViewModel
。
eg 例如
public class LoginViewModel
{
public string Email { get; set; }
public string Password { get; set; }
}
public class RegisterViewModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
Do I need to make another ViewModel which holds these 2 ViewModels? 我是否需要制作另一个包含这两个ViewModel的ViewModel?
public BigViewModel
{
public LoginViewModel LoginViewModel{get; set;}
public RegisterViewModel RegisterViewModel {get; set;}
}
I need the validation attributes to be brought forward to the view. 我需要将验证属性提交给视图。 This is why I need the ViewModels. 这就是为什么我需要ViewModels的原因。
Isn't there another way such as (without the BigViewModel
): 是否没有其他方法,例如(没有BigViewModel
):
@model ViewModel.RegisterViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Name)
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
@model ViewModel.LoginViewModel
@using (Html.BeginForm("Login", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(model => model.Email)
@Html.PasswordFor(model => model.Password)
}
#1楼
参考:https://stackoom.com/question/JzKt/视图中的多个模型
#2楼
Another way is to use: 另一种方法是使用:
@model Tuple<LoginViewModel,RegisterViewModel>
I have explained how to use this method both in the view and controller for another example: Two models in one view in ASP MVC 3 我已经为另一个示例说明了如何在视图和控制器中使用此方法: ASP MVC 3中一个视图中的两个模型
In your case you could implement it using the following code: 您可以使用以下代码实现它:
In the view: 在视图中:
@using YourProjectNamespace.Models;
@model Tuple<LoginViewModel,RegisterViewModel>
@using (Html.BeginForm("Login1", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(tuple => tuple.Item2.Name, new {@Name="Name"})
@Html.TextBoxFor(tuple => tuple.Item2.Email, new {@Name="Email"})
@Html.PasswordFor(tuple => tuple.Item2.Password, new {@Name="Password"})
}
@using (Html.BeginForm("Login2", "Auth", FormMethod.Post))
{
@Html.TextBoxFor(tuple => tuple.Item1.Email, new {@Name="Email"})
@Html.PasswordFor(tuple => tuple.Item1.Password, new {@Name="Password"})
}
Note that I have manually changed the Name attributes for each property when building the form. 请注意 ,在构建表单时,我已经手动更改了每个属性的Name属性。 This needs to be done, otherwise it wouldn't get properly mapped to the method's parameter of type model when values are sent to the associated method for processing. 这需要完成,否则当将值发送到关联的方法进行处理时,它将无法正确映射到类型模型的方法参数。 I would suggest using separate methods to process these forms separately, for this example I used Login1 and Login2 methods. 我建议使用单独的方法分别处理这些表单,对于本示例,我使用了Login1和Login2方法。 Login1 method requires to have a parameter of type RegisterViewModel and Login2 requires a parameter of type LoginViewModel. Login1方法要求参数类型为RegisterViewModel,而Login2方法要求参数类型为LoginViewModel。
if an actionlink is required you can use: 如果需要一个动作链接,则可以使用:
@Html.ActionLink("Edit", "Edit", new { id=Model.Item1.Id })
in the controller's method for the view, a variable of type Tuple needs to be created and then passed to the view. 在用于视图的控制器方法中,需要创建一个类型为Tuple的变量,然后将其传递给视图。
Example: 例:
public ActionResult Details()
{
var tuple = new Tuple<LoginViewModel, RegisterViewModel>(new LoginViewModel(),new RegisterViewModel());
return View(tuple);
}
or you can fill the two instances of LoginViewModel and RegisterViewModel with values and then pass it to the view. 或者您可以使用值填充LoginViewModel和RegisterViewModel的两个实例,然后将其传递给视图。
#3楼
a simple way to do that 一种简单的方法
we can call all model first 我们可以先打电话给所有模特
@using project.Models
then send your model with viewbag 然后使用Viewbag发送模型
// for list
ViewBag.Name = db.YourModel.ToList();
// for one
ViewBag.Name = db.YourModel.Find(id);
and in view 并鉴于
// for list
List<YourModel> Name = (List<YourModel>)ViewBag.Name ;
//for one
YourModel Name = (YourModel)ViewBag.Name ;
then easily use this like Model 然后像模型一样轻松使用
#4楼
I want to say that my solution was like the answer provided on this stackoverflow page: ASP.NET MVC 4, multiple models in one view? 我想说的是,我的解决方案就像在stackoverflow页面上提供的答案: ASP.NET MVC 4,在一个视图中有多个模型?
However, in my case, the linq query they used in their Controller did not work for me. 但是,就我而言,他们在Controller中使用的linq查询对我不起作用。
This is said query: 这就是查询:
var viewModels =
(from e in db.Engineers
select new MyViewModel
{
Engineer = e,
Elements = e.Elements,
})
.ToList();
Consequently, "in your view just specify that you're using a collection of view models" did not work for me either. 因此,“在您的视图中仅指定您正在使用视图模型的集合”对我也不起作用。
However, a slight variation on that solution did work for me. 但是,对该解决方案进行细微改动确实对我有用。 Here is my solution in case this helps anyone. 这是我的解决方案,以防万一。
Here is my view model in which I know I will have just one team but that team may have multiple boards (and I have a ViewModels folder within my Models folder btw, hence the namespace): 这是我的视图模型,我知道我只有一个团队,但是那个团队可能有多个面板(并且我的Models文件夹btw中有一个ViewModels文件夹,因此是名称空间):
namespace TaskBoard.Models.ViewModels
{
public class TeamBoards
{
public Team Team { get; set; }
public List<Board> Boards { get; set; }
}
}
Now this is my controller. 现在这是我的控制器。 This is the most significant difference from the solution in the link referenced above. 这是与上面引用的链接中的解决方案最大的不同。 I build out the ViewModel to send to the view differently. 我建立了ViewModel以不同的方式发送到视图。
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
TeamBoards teamBoards = new TeamBoards();
teamBoards.Boards = (from b in db.Boards
where b.TeamId == id
select b).ToList();
teamBoards.Team = (from t in db.Teams
where t.TeamId == id
select t).FirstOrDefault();
if (teamBoards == null)
{
return HttpNotFound();
}
return View(teamBoards);
}
Then in my view I do not specify it as a list. 然后在我看来,我没有将其指定为列表。 I just do "@model TaskBoard.Models.ViewModels.TeamBoards" Then I only need a for each when I iterate over the Team's boards. 我只是执行“ @model TaskBoard.Models.ViewModels.TeamBoards”,然后在团队团队中进行迭代时,每个都需要一个。 Here is my view: 这是我的看法:
@model TaskBoard.Models.ViewModels.TeamBoards
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Team</h4>
<hr />
@Html.ActionLink("Create New Board", "Create", "Board", new { TeamId = @Model.Team.TeamId}, null)
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => Model.Team.Name)
</dt>
<dd>
@Html.DisplayFor(model => Model.Team.Name)
<ul>
@foreach(var board in Model.Boards)
{
<li>@Html.DisplayFor(model => board.BoardName)</li>
}
</ul>
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Team.TeamId }) |
@Html.ActionLink("Back to List", "Index")
</p>
I am fairly new to ASP.NET MVC so it took me a little while to figure this out. 我对ASP.NET MVC相当陌生,因此花了我一些时间才弄清楚这一点。 So, I hope this post helps someone figure it out for their project in a shorter timeframe. 因此,我希望这篇文章能帮助某人在较短的时间内为他们的项目弄清楚。 :-) :-)
#5楼
Use a view model that contains multiple view models: 使用包含多个视图模型的视图模型:
namespace MyProject.Web.ViewModels
{
public class UserViewModel
{
public UserDto User { get; set; }
public ProductDto Product { get; set; }
public AddressDto Address { get; set; }
}
}
In your view: 您认为:
@model MyProject.Web.ViewModels.UserViewModel
@Html.LabelFor(model => model.User.UserName)
@Html.LabelFor(model => model.Product.ProductName)
@Html.LabelFor(model => model.Address.StreetName)
#6楼
Create one new class in your model and properties of
LoginViewModel
andRegisterViewModel
: 在模型和LoginViewModel
和RegisterViewModel
属性中创建一个新类:public class UserDefinedModel() { property a1 as LoginViewModel property a2 as RegisterViewModel }
Then use
UserDefinedModel
in your view. 然后在您的视图中使用UserDefinedModel
。