【问题标题】:Why I can't get to see my selected information on another page?为什么我无法在另一个页面上看到我选择的信息?
【发布时间】:2017-01-11 09:51:48
【问题描述】:

我正在处理一个简单的电话簿,并且正在尝试删除一个联系人。当用户按下左图上的删除时,它的信息将不会显示(右图)

这是我在控制器中的代码

[HttpPost]
public ActionResult Delete(Models.EF_Model.Phone_book _Model)
{
    if (ModelState.IsValid)
    {
        Ref_ViewModel = new ViewModel.ViewModel();
        Ref_ViewModel.Delete(_Model.Id);
    }
    else
    {
        ViewBag.Massage = "Choose a Contact";
    }
    return View();
}

这是我的看法

@model Project1.Models.EF_Model.Phone_book

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
@ViewBag.Massage
<div>
    <h4>Phone_book</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Id)
        </dt>

        <dd>
            @Html.DisplayFor(m => m.Id)
        </dd>

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

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

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

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

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

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

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

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

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

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

    </dl>

    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.HiddenFor(model => model.Id)
        <div class="form-actions no-color">
            <input type="submit" value="Delete" class="btn btn-default" /> |
            @Html.ActionLink("Back to List", "Index")
        </div>
    }
</div>

【问题讨论】:

  • 你需要展示GET方法

标签: c# html asp.net-mvc razor view


【解决方案1】:

您没有将任何模型返回到视图。

 return View(); <--- model is missing.

应该是

 var model = _someRepository.LoadPhoneBook(id);
 return View(model);

【讨论】:

  • 你的意思是在我的 [get] 方法中?
  • @MostafaBouzari 您在 get 和 post 方法中都需要它。如果你的帖子因为无效的Modelstate而无法删除,你仍然需要将模型返回到你的视图中,以便它可以再次渲染它。
猜你喜欢
  • 2016-03-08
  • 2011-03-25
  • 2013-11-13
  • 1970-01-01
  • 2012-09-10
  • 2021-09-10
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多