Mostafa Bouzari
Mostafa Bouzari

Reputation: 10240

The parameters dictionary contains a null entry for parameter '_id' of non-nullable type 'System.Int32'

(it's a simple phonebook project)i want to delete a row in a list (a contact from a notebook) and when user presses delete button i want to show it's information and get a confirmation from user here is view code

    @model Project1.Models.EF_Model.Phone_book
@{
    ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Product</h4>
    <hr />
    <dl class="dl-horizontal">
        <tr>
        <dt>
            @Html.DisplayNameFor(model => model.First_Name)
        </dt>
        <td>
            @Html.DisplayFor(model => model.First_Name)
        </td>
        <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>
    </tr>
    </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>

and in my controller i chek the basics and send id to the model to get it deleted

[HttpGet]
public ActionResult Delete(int? _id)
{
    return View();
}
#endregion

#region [- Post -]

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

how can i show my selected data in my confirmation view?

when i press the button to delete it and also i recieve this error

The parameters dictionary contains a null entry for parameter '_id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Delete(Int32)' in 'Project1.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

when i press the button to delete it,i guess it returns null value or i have to do some thing with routconfig,pls help me i'm i'm working o it for 2 days(U should know that i've just started learning mvc )

Upvotes: 0

Views: 7237

Answers (2)

FirstCall
FirstCall

Reputation: 84

MVC is telling you that you have method which takes a non null parameter of int _id and no value was provided. This means that when data was sent to the web server, the form collection contained no value named _id.

When you submit a form in HTML, the only data that gets sent to the server by default are elements. So, in order to send the _id value to the server, you can create an element inside your form with a name of _id.

MVC has helper functions to create inputs for you based on your view model. @Html.HiddenFor(model => model._id).

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

Good luck learning MVC. I would encourage you to try the tutorials found here. http://www.asp.net/mvc/overview/getting-started/introduction/getting-started

Upvotes: 1

Kolichikov
Kolichikov

Reputation: 3020

You are doing a form submit and your action is labelled HttpPost, so what will come back to mvc is a Project1.Models.EF_Model.Phone_book object.

Nothing in your view is telling the controller that it should get an _id parameter, so when Mvc binds the parameters, it doesn't bind anything to _id.

To solve this, see this answer for a way to pass the _id back to your controller.

Upvotes: 1

Related Questions