Mostafa Bouzari Mostafa Bouzari - 3 months ago 11
C# Question

Why I can't get to see my selected information on another page?

I'm working on a simple phone book and I'm trying to delete a Contact. When user presses the delete on Left picture it's information won't get to be shown (Right pic)

enter image description here

Here's my code in controller

[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();
}


This is my 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>

Answer

You are not returning any model to a view.

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

Should be

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