GeekyNuns
GeekyNuns

Reputation: 298

null entry for parameter 'id'

All the similar topics are resolved ,but I cant find similar mistake as mine.

Model :

public class CountriesViewModel
{
    public int BuffId { get; set; }
    public string CountryId { get; set; }
    public string Name { get; set; }
}

View :

    @model List<BTGHRM.Models.CountriesViewModel>
    @{ 
        WebGrid grid = new WebGrid(Model, canSort: false, rowsPerPage: 15);
        int row = 0;
    }


    @if (Model.Any())
    {

        @grid.GetHtml(
                tableStyle: "table",
                headerStyle: "table_HeaderStyle",
                footerStyle: "table_PagerStyle",
                rowStyle: "table_RowStyle",
                alternatingRowStyle: "table_AlternatingRowStyle",
                selectedRowStyle: "table_SelectedRowStyle",
                columns: grid.Columns(

                grid.Column("Name", @Resources.Localization.country, format: @<text>
                        <span class="display-mode"><label id="NameLabel">@item.Name</label></span>
                        @Html.TextBox("Model[" + (++row - 1).ToString() + "].Name", (object)item.Name, new { @class = "edit-mode" })
                </text>, style: "p40"),

                      grid.Column("", "", format: @<text>
                        @Html.Hidden("Model[" + (row - 1).ToString() + "].BuffId", (object)item.BuffId, new { @class = "edit-mode" })
                    </text>, style: "p13"),

            grid.Column("", "", format: @<text>
                        <a href="DeleteCountryRecord/@item.BuffId" id="@item.BuffId" class="link_button delete-button display-mode">@Resources.Localization.delete</a>
            </text>)
                )
            )
    }

I`d like to delete row by sending its id (buffId) in action method :

public ActionResult DeleteCountryRecord(int BuffId)
    {
        using (var db = new HRMEntities())
        {
            Country RemovableLine = db.Countries.Find(BuffId);
            try
            {
                db.Countries.Remove(RemovableLine);
            }
            catch
            {
                TempData["Message"] = App_GlobalResources.Localization.error;
            }
            db.SaveChanges();
        }
        return RedirectToAction("CountryCodes");
    }

I get all the Ids right, but i have mistake

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

But in the link which is genered on click of the button can see, that id is posted right :

http://localhost:59763/Administration/DeleteCountryRecord/17

What could be my problem?

Upvotes: 0

Views: 158

Answers (1)

Joe Enos
Joe Enos

Reputation: 40431

You have to ensure a route is defined that specifically names BuffId. That, or you can rename the parameter to id in your action method signature, assuming that default is in your routing table.

If you add a new route, it would go with the rest of your routing (probably RouteConfig.cs), and make sure it goes above the default one:

routes.MapRoute(
    name: "DeleteCountryRecord",
    url: "Administration/DeleteCountryRecord/{BuffId}",
    defaults: new { controller = "Administration", action = "DeleteCountryRecord" }
);

Upvotes: 3

Related Questions