【问题标题】:MVC 5 dynamic model in partial view局部视图中的 MVC 5 动态模型
【发布时间】:2019-07-20 11:35:07
【问题描述】:

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

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

我还为每个视图创建了视图模型、控制器和 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>
           ..
           ..

【问题讨论】:

  • 如何为所有这些创建一个通用视图模型(例如 LookupViewModel)?
  • 我为每个表创建了数据模型,并在视图模型中被引用。我有每个数据库操作的存储库。需要重写一般所有并传递变量以区分视图。所以变量来自视图 =>controller=vm=>dm=>repository,它根据变量进行 db 操作?

标签: model-view-controller asp.net-mvc-5 asp.net-mvc-partialview


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-09-20
    • 2014-06-27
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多