【问题标题】:Displaying data in a view returned from a controller在控制器返回的视图中显示数据
【发布时间】:2017-08-29 20:19:42
【问题描述】:

我正在尝试编写一个简单的 Web 应用程序,它根据从选择列表中选择的 ID 显示某些详细信息。

我可以使用以下方法从数据库中获取数据:

类:

namespace InterviewTest.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;
[Table("Widget")]

        public partial class Widget
        {
            public int WidgetID { get; set; }

            [Required]
            [StringLength(50)]
            public string WidgetName { get; set; }

            [Required]
            public string WidgetDescription { get; set; }

            public int WidgetColourID { get; set; }
        }

}

控制器:

using System.Linq;
using System.Web.Mvc;
using InterviewTest.Models;

namespace InterviewTest.Controllers
{
    public class HomeController : Controller
    {
        WidgetConn db = new WidgetConn();

        public virtual ActionResult Index(Widget widget)
        {


            var widgets =
                (from w in db.Widgets
                 where w.WidgetID == 1
                 select new
                    {
                     WidgetName = w.WidgetName,
                     WidgetDescription = w.WidgetDescription,
                     WidgetColourID = w.WidgetColourID
                    }).ToList();

            var data = widgets[0];

            return View(data);
        }
    }
}

'data'中返回的数据返回为:

小部件ID 1 小部件描述测试 WidgetColourID 1

测试视图:

@model InterviewTest.Models.Widget
@{
    ViewBag.Title = "test";
}

<h2>test</h2>

<div>
    <h4>Widget</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.WidgetName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.WidgetDescription)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetDescription)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.WidgetColourID)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.WidgetColourID)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.WidgetID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

当我尝试使用 @Html.DisplayNameFor(model => model.WidgetName) 来显示返回的数据时,我得到了错误:

htmlhelper 不包含 DisplayNameFor 的定义,并且找不到扩展方法 DisplayNameFor 接受类型为“HTMLHelper”的第一个参数。您是否缺少汇编或参考?

我读到我应该'使用 System.Web.Mvc.HtmlHelper;'但是,这似乎是一个名称空间,因此不能以这种方式使用。更改我的命名空间意味着我无法再访问此类。

我还读到 System.Web.Mvc.Html 应该包含“HtmlHelper”,但使用它并没有解决我的问题。

我的问题是:

1:我应该将 @Html.DisplayNameFor(model => model.WidgetName) 与 .cshtml 文件一起使用,还是有其他方法可以访问数据?

2:如果我尝试访问数据的方式是正确的,我应该如何/在哪里添加命名空间,以便我可以访问 HtmlHelper 命名空间。

3:如果我尝试访问数据的方式对于 .cshtml 文件不正确,请有人指出我可以阅读的一些文档吗?

谢谢。

【问题讨论】:

  • 你试过脚手架吗?,你的看法
  • 感谢您的回复,我之前使用过脚手架,但这次我尝试手动操作,以更好地了解一切是如何工作的。理想情况下,我希望能够手动完成脚手架/EF 所做的工作,因为我正在尝试学习和理解编写代码时发生了什么以及运行它时会发生什么。

标签: c# razor


【解决方案1】:

我认为这是因为您将数据选择为未知类型。而不是未知,指定它的类型。所以请替换你的代码,如下所示。

 public virtual ActionResult Index(Widget widget)
        {
            IEnumerable<Widget> widgets =
                (from w in db.Widgets
                 where w.WidgetID == 1
                 select new Widget 
                    {
                     WidgetName = w.WidgetName,
                     WidgetDescription = w.WidgetDescription,
                     WidgetColourID = w.WidgetColourID
                    }).ToList();

            var data = (widgets!=null && widgets.length>0)? widgets[0]: null;

            return View(data);
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多