如何在其他页面上显示我选择的项目?

时间:2016-09-04 07:50:54

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

我的项目是一本简单的电话簿,我想删除我的(家庭控制器)索引中的联系人(左图)并将其信息发送到确认页面(右图)我想显示所选联系人的数据

(例如,如果你删除了gjgj,它的信息会显示在右侧)

enter image description here

这是我的观点

@model IEnumerable< 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(model =>model.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>

这是索引代码(左图)

@model IEnumerable<Project1.Models.EF_Model.Phone_book>
@{
    ViewBag.Title = "Index";
}
<br /><br />
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Last_Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Number)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Email)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Address)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.First_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Last_Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Number)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Email)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Address)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
                @Html.ActionLink("Details", "Index", new { id = item.Id }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.Id })
            </td>
        </tr>
    }

</table>

这是我在我看来使用的模型

    public partial class Phone_book
{
    public int Id { get; set; }
    public string First_Name { get; set; }
    public string Last_Name { get; set; }
    public string Number { get; set; }
    public string Email { get; set; }
    public string Address { get; set; }
}

这是我的获取和发布代码

   [HttpGet]
  //  [HttpDelete]
    public ActionResult Delete(int? _id)
    {
        Ref_ViewModel = new ViewModel.ViewModel();
        var i = Ref_ViewModel.Select(_id);
        return View(i);

    }
    #endregion

    #region [- Post -]

    [HttpPost]
    //[HttpDelete]
    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);
    }
    #endregion

我该怎么做才能在这部分中显示我选择的联系人?

1 个答案:

答案 0 :(得分:2)

按如下方式使用ActionLink重定向到删除视图中带有ID的另一个页面:

@Html.ActionLink("Add To Cart", "ProductDetails", "Product", new { id = item.ProductId }, new { @class = "btn btn-default", alt = "@Model.ProductName" })

最后使用以下内容从控制器中的url获取id并获取删除视图中的详细信息:

int id = Convert.ToInt32(Url.RequestContext.RouteData.Values["id"]); 

请检查链接:http://kristianguevara.net/creating-your-asp-net-mvc-5-application-from-scratch-for-beginners-using-entity-framework-6-and-identity-with-crud-functionalities/