局部视图中的MVC 5动态模型

问题描述 投票:0回答:1

我有一些(大约6个)查找表,如degreetype,categorytype等。所有这些表都有类似的列id,名称和isactive字段。我为每个表创建了CRUD存储过程。

在我的MVC5项目中,我创建了一个模型,存储库和DAL,以便为每个传递数据到sp。

我还为每个创建了viewmodel,controller和crud视图。

我意识到crud(创建/编辑/详细/列表/删除)视图页面使用相同的html,除了在页面顶部调用模型。

问题:有什么办法可以创建html的局部视图并在页面中使用动态模型吗?

例如:

@model Microsoft.myorg.viewmodels.degreetypesVM
<!-- need to use a dynamic model (degreetypesVM, categorytypeVM etc) above and create a partial view for the below html -->

@{
    ViewBag.Title = "Degree Type";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm())
{
 <table class="container">
        <tbody>
            <tr class="row">
                <td class="col2" scope="row">
                    <div class="title-text">
                        @Html.LabelFor(model => model.Name)
                        <span title="This field is required." class="warning">*</span>
                    </div>
                </td>
                <td class="gray">
                    @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
                </td>
            </tr>
            ..
            ..
           </tbody>
           ..
           ..
model-view-controller asp.net-mvc-5 asp.net-mvc-partialview
1个回答
0
投票

你可以简单地编写局部视图并在其中传递模型,或者只是包装所有常见文件并为部分视图编写自己的模型

© www.soinside.com 2019 - 2024. All rights reserved.