【问题标题】:Model is not showing any values in .cshtml page?模型在 .cshtml 页面中没有显示任何值?
【发布时间】:2020-03-30 07:01:08
【问题描述】:

我正在使用 ViewModel,它必须在 [HttpGet] 请求中从三个表中获取和设置方法我只想从一个表中获取名称-值对并将它们显示在标签上,但是当我将对象返回到 .cshtml页面此对象没有显示任何内容,但是当我使用断点检查控制器中的值时,它具有所有值。

    [HttpGet]
            public ActionResult CreateNewAsset()
            {
                List<AssetVM> assetVMs = db.Physical_Asset_Properties.Select(x => new AssetVM
                {
                    PAP_Id=x.PAP_Id,
                   PAP_Value=x.PropertyName
                }).ToList();
                ViewBag.Message = "This Page is related to assets";
                return View(assetVMs);
            }

//这是查看页面

@model List<OmniConnect.ViewModel.AssetVM.AssetVM>
<div id="phy_asset_properties" style="display:none">

                                <div class="row">
                                    @foreach(var item in Model)
                                    {
                                        <div class="col-sm-4">
                                            <div class="form-group">
                                                <label>@item.PropertyName</label>
                                                <input type="text" class="form-control" placeholder="" id="@item.PAP_Id">
                                            </div>
                                        </div>
                                    }
                                </div>

                            </div>

【问题讨论】:

  • 当你断点return View(assetVMs)assetVMs会显示什么?
  • 它显示了我想要的所有 5 个名称值对,但在视图中,id 正在呈现但名称没有显示

标签: c# .net asp.net-mvc


【解决方案1】:

AssetVM 中使用 PAP_Value 属性,在 View 中使用原始类中的 PropertyName 属性。

应该是:

@foreach(var item in Model)
{
    <div class="col-sm-4">
        <div class="form-group">
            <label>@item.PAP_Value</label>
            <input type="text" class="form-control" placeholder="" id="@item.PAP_Id">
        </div>
    </div>
}

【讨论】:

  • 哈哈,不客气。我已经看了好几个小时之前的屏幕了,这很简单:)
  • 我也是这样
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-10
  • 2014-08-31
  • 2021-07-11
  • 2020-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多