如果 ViewModel 中只有一个属性,它真的有必要吗?

问题描述

我的 MVC (.NET 5) 应用程序中有一个名为“资产/索引”的视图,我在其中显示资产表中的资产列表。该页面以 IList<Asset> 为模型,即 @model IList<Asset>。我的同行建议我为该视图制作一个 ViewModel,但在这种情况下真的需要一个 ViewModel 吗?我的意思是我看到的唯一优势是,向页面添加另一个属性(如果将来需要)很容易,因为在这种情况下我肯定需要一个 ViewModel。但是,由于该页面的唯一目的是显示资产列表(我很确定将来不需要向该假设的 ViewModel 添加属性),我很困惑是遵循我的直觉还是同行的建议。


视图:

@using AdminPortal.Models;
@model IReadOnlyList<Asset>
@{ 
    ViewData["Title"] = "List";

}

@{ await Html.RenderPartialAsync("../Partials/SearchPartial","AssetsController"); }

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type="text/javascript">
    document.getElementById('list-btn').hidden = true;
</script>

<p>
    <a asp-action="Create" class="btn-link">Create New</a>
</p>

<table class="table">
    <thead class="thead-light">
        <tr>
            <th>@Html.DisplayNameFor(model => model.First().Id)</th>
            <th>@Html.DisplayNameFor(model => model.First().Address)</th>
            <th>@Html.DisplayNameFor(model => model.First().State)</th>
            <th>@Html.DisplayNameFor(model => model.First().Temperature)</th>
            <th>@Html.DisplayNameFor(model => model.First().Moisture)</th>
            <th>@Html.DisplayNameFor(model => model.First().Alerts)</th>
            <th>@Html.DisplayNameFor(model => model.First().LastServiced)</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var asset in Model)
        {
            <tr>
                @if (asset.State != AssetState.deleted)
                {
                    <td>
                        <a asp-action="Index" asp-controller="Home"
                           asp-route-id="@asset.Id">@Html.DisplayFor(item => asset.Id)</a>
                    </td>
                }
                else
                {
                    <td>
                        <a>@Html.DisplayFor(item => asset.Id)</a>
                    </td>
                }
                <td>@Html.DisplayFor(item => asset.Address)</td>
                <td>
                    @if (asset.State != AssetState.deleted)
                    {
                        var IdName = "assetStateDropdown" + asset.Id;
                        <select class="dropdown" id="@IdName">
                            <option class="dropdown-item dropdown-item-text"
                                    value="@AssetState.functional">
                                functional
                            </option>
                            <option class="dropdown-item dropdown-item-text"
                                    value="@AssetState.non_functional">
                                non-functional
                            </option>
                            <option class="dropdown-item dropdown-item-text"
                                    value="@AssetState.under_maintenance">
                                under-maintenance
                            </option>
                        </select>
                        <script type="text/javascript">
                            //change the state of the asset from the dropdown
                            $(function () {
                                $("#@IdName").change(function () {
                                    $.ajax({
                                        type: "POST",url: "/Assets/UpdateState",data: {
                                            "state": $("#@IdName").val(),"id": '@asset.Id'
                                        },success: function (response) {
                                            alert("State Changed");
                                        },failure: function (response) {
                                            alert("Could not Change state");
                                        },error: function (response) {
                                            alert("Some error occurred. Try again later");
                                        }
                                    });
                                });
                            });
                        </script>
                        <script type="text/javascript">
                            //set the value of the asset state drop down to the current state
                            document.getElementById('@IdName').value = '@asset.State';
                        </script>
                    }
                    else
                    {
                        <p>deleted</p>
                    }
                </td>
                <td>@Html.DisplayFor(item => asset.Temperature)</td>
                <td>@Html.DisplayFor(item => asset.Moisture)</td>
                <td>
                    <a asp-action="Index" asp-controller="Alerts"
                       asp-route-id="@asset.Id">@Html.DisplayFor(item => asset.Alerts.Count)</a>
                </td>
                <td>@Html.DisplayFor(item => asset.LastServiced)</td>
                @if (asset.State != AssetState.deleted)
                {
                    <td>
                        <a asp-action="Delete" asp-route-id="@asset.Id" class="btn-link" style="color: darkred">Decommission</a>
                    </td>
                }
            </tr>
        }
    </tbody>
</table>

建议的视图模型:

public class IndexViewModel
{
    public IReadOnlyList<Asset> Assets { get; set; }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)