Mostafa Bouzari
Mostafa Bouzari

Reputation: 10240

Can't see my record information in my View

i'm tryng to delete a record (it's a simple phone book project) and show it's data in confirmation page but now it looks like this

How can i fill the blanks

this is my home controller

    #region [- Get -]

    [HttpGet]
    //  [HttpDelete]
    public ActionResult Delete(int? _id, Models.EF_Model.Phone_book _model)
    {
        return View();
    }
    #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
    #endregion

this is it's view

@model Phone_Book.Models.EF_Model.Phone_book

@{
    ViewBag.Title = "Delete";
}

<h2>Delete</h2>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Phone_book</h4>
    <hr />
    <dl class="dl-horizontal">
        <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()

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

i've tried to fix this by my self but couldn't think of anything

how can i fill the blanks?

Upvotes: 1

Views: 41

Answers (2)

Antoine Delia
Antoine Delia

Reputation: 1845

The problem is that you send nothing from your controller to your view.

You should find your contact based on your id. Once it's done, check if it is not null and send it to your view.

[HttpGet]
public ActionResult Delete(int? id)
{
    if (id == null)
    {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Phone_Book.Models.EF_Model.Phone_book‌ contact = Context.Phone_book.Select(_id);
    if (contact == null)
    {
         return HttpNotFound();
    }
    return View(contact);
}

Upvotes: 1

David
David

Reputation: 219016

You're not sending an instance of the model to the view:

public ActionResult Delete(int? _id, Models.EF_Model.Phone_book _model)
{
    return View();
}

So there's nothing to display. Normally I wouldn't suspect this action to take an instance of the model as an argument. When you debug this, does _model have the instance you're looking for? If so, pass it to the view:

return View(_model);

If not, you can use _id to look up the model from your data source and pass that to the view:

var model = **query your DB here**;
return View(model);

(If even _id isn't populated with anything useful then it sounds like you would have another issue somewhere else to be solved as well.)

Upvotes: 0

Related Questions