参数' id'的空条目

时间:2016-07-01 16:07:28

标签: c# asp.net-mvc dictionary

所有相似的主题都已解决,但我找不到类似的错误。

型号:

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

查看:

    @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>)
                )
            )
    }

我想通过在action方法中发送id(buffId)来删除行:

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");
    }

我得到了所有的ID,但我有错误

  

参数字典包含参数&#39; BuffId&#39;的空条目。非可空类型的System.Int32&#39; for method&#39; System.Web.Mvc.ActionResult DeleteCountryRecord(Int32)&#39;在&#;; BTGHRM.Controllers.AdministrationController&#39;。可选参数必须是引用类型,可空类型,或者声明为可选参数。   参数名称:参数

但是在单击按钮时所生成的链接中可以看到,该ID是正确发布的:

  

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

可能是我的问题?

1 个答案:

答案 0 :(得分:3)

您必须确保定义了一个专门命名为BuffId的路由。那个,或者你可以在操作方法签名中将参数重命名为id,假设默认值在你的路由表中。

如果添加新路由,它将与路由的其余部分(可能是RouteConfig.cs)一起使用,并确保它超出默认路由:

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