在不使用ViewModel对象的情况下,Razor视图可以具有多个模型吗? [英] Can a Razor view have more than one model without using a ViewModel object?

查看:79
本文介绍了在不使用ViewModel对象的情况下,Razor视图可以具有多个模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@model MyMVC.Models.MyMVC.MyModel

@{
    ViewBag.Title = "Index";
}

我问这个问题的原因是在MVC中,我们可以有多个表格,所以我想为每个表格建立一个模型.但是,当我尝试在视图顶部添加另一个模型时,它显示了一个错误.我知道我们可以使用 ViewModel ,该模型内部具有model1和model2,但这不是我要问的问题.

The reason why I ask this question is in MVC we can have more than 1 form, so I would like to have a model for each form. But when I was trying to add another model at the top of the view, it displays an error. I know we can use ViewModel which has model1 and model2 inside, but it's not the question I am asking.

我只是想知道有什么方法可以将2个模型放到1个视图中.

I just simply want to know is there any way that we can put in 2 models into 1 view.

更新:

例如,我希望视图中有2个表单,因此对于每种表单,我都希望有一个单独的模型.

For example, I want to have 2 forms in my view, so for each form I'd like to have a separated model.

更新2 谢谢大家的回复.我现在知道MVC在一个视图上仅支持一个单一模型.你们认为这是MVC的劣势吗?为什么或为什么不呢?(还没有人回答...为什么?)

Update 2 Thank you all for your replies. I now know that MVC only supports one single model on one view. Do you guys consider this a disadvantage of MVC? Why or Why not?(No one has answered it yet.... why?)

您的答案相似,所以我什至不知道应该将哪个答案设为答案.

Your answers are similar, so I don't even know which one should be set as an answer.

推荐答案

对于其他模型,应使用一些prtialviews.您具有主模型的主视图.在该视图内部,您可以对它们的模型和验证进行部分局部查看.

You should use some prtialviews for other models. You have your main view with main model. Inside of that view, you could have some partialviews with their models and their validations.

--------------------------------------

--------------------------------------

正如其他人所说,最好使用视图模型并将模型彼此组合.但是根据您的需要,我在我的Github帐户上为您创建了一个示例项目:

As others said it is better to use View Models and combine the models with each others. But as you wanted I created a sample project for you on my Github account:

https://github.com/bahman616/ASPNET_MVC_multiple_models_in_a_view_with_partialview.git

这是该项目的简化代码:

Here is the simplified code of that project:

有两种型号:

public partial class Person
{
    public int ID { get; set; }
    [Required]
    public string Name { get; set; }
}
public partial class Company
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
}

我希望两个视图都在一个视图中创建,所以这是父视图:

I want to have both create views in one view, so this is the parent view:

@model ASP_NET_MVC_Multiple_Models_In_A_View.Models.Person

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm("Create","Person")) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Person</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@{Html.RenderAction("_CompanyCreate", "Company");}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

这是局部视图:

@model ASP_NET_MVC_Multiple_Models_In_A_View.Models.Company

<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>

@using (Html.BeginForm("_CompanyCreate","Company")) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Company</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这篇关于在不使用ViewModel对象的情况下,Razor视图可以具有多个模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆