【问题标题】:null entry for parameter 'id'参数“id”的空条目
【发布时间】:2016-07-01 16:07:27
【问题描述】:

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

型号:

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 都正确,但我有错误

参数字典包含“BTGHRM.Controllers.AdministrationController”中方法“System.Web.Mvc.ActionResult DeleteCountryRecord(Int32)”的不可为空类型“System.Int32”的参数“BuffId”的空条目。可选参数必须是引用类型、可空类型或声明为可选参数。 参数名称:参数

但是在点击按钮生成的链接中可以看到,那个id是正确的:

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

我的问题可能是什么?

【问题讨论】:

    标签: c# asp.net-mvc dictionary


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-05-12
      • 2012-08-25
      • 1970-01-01
      • 2012-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-06
      相关资源
      最近更新 更多