【问题标题】:MVC Details page not showing any dataMVC 详细信息页面未显示任何数据
【发布时间】:2018-04-27 18:10:39
【问题描述】:

我对 MVC 很陌生,我正在构建一个小型联系人应用程序。我已经创建了数据库,索引页面显示了数据库的数据。我遇到的问题是我的详细信息页面。它会显示标题,但不会显示任何数据。

这是我的模型:

namespace BCM.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string CompanyName { get; set; }
        public string Address { get; set; }
        public string Address2 { get; set; }
        public string Address3 { get; set; }
        public string City { get; set; }
        public string County { get; set; }
        public string Country { get; set; }
        public string PostCode { get; set; }
        public string Telephone { get; set; }
        public string Telephone1 { get; set; }
        public string Telephone2 { get; set; }
        public string Telephone3 { get; set; }
        public string Telephone4 { get; set; }
        public string Email { get; set; }
        public string Email2 { get; set; }
        public string Website { get; set; }
        public string Notes { get; set; }
    }
}

这是我的控制器:

public ActionResult Details()
    {
        return View();
    }
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Details(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Contact contact = db.Contacts.Find(id);
        if (contact == null)
        {
            return HttpNotFound();
        }

        return View(contact);
    }

这是我的观点的一部分:

@model BCM.Models.Contact

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

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

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

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

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

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

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

这是我的详细信息页面显示的内容: enter image description here

提前致谢

【问题讨论】:

  • 找出我所缺少的并在下面发布了答案
  • 我不明白为什么在尝试显示模型时使用 [HttpPost] 属性。您必须改用 [HttpGet] 方法

标签: asp.net-mvc


【解决方案1】:

设法弄清楚我错过了什么。这与我的 @Html.ActionLink 没有通过 Id 有关

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-24
    • 2014-09-26
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    相关资源
    最近更新 更多