簡體   English   中英

參數字典包含一個 null 條目,用於不可為空類型“System.Int32”的參數“id”

[英]The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'

我正在構建我的第一個 MVC 應用程序,我在數據庫中有一個包含 3 列的表:

  1. Id → 主鍵
  2. 用戶名
  3. 密碼

當我點擊編輯鏈接編輯記錄時,它拋出以下異常:

對於“MvcApplication1.Controllers.UserController”中的方法“System.Web.Mvc.ActionResult Edit(Int32)”,參數字典包含不可為空類型“System.Int32”的參數“id”的 null 條目。 可選參數必須是引用類型、可空類型或聲明為可選參數。 參數名稱:參數

這是我的編輯代碼:

public ActionResult Edit(int id, User collection)
{
    UserDBMLDataContext db = new UserDBMLDataContext();
    var q = from abc in db.User_Login_Details
            where abc.Id == id
            select abc;

    IList lst = q.ToList();

    User_Login_Details userLook = (User_Login_Details)lst[0];

    userLook.Username = collection.UserName;
    userLook.Password = collection.Password;
    db.SubmitChanges();
    return RedirectToAction("Index");                  
}

您希望在您的 URL 中有一個id參數,但您沒有提供一個。 如:

http://yoursite.com/controller/edit/12
                                    ^^ missing

在你的WebApiConfig >> Register ()你必須改成

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }

這里的routeTemplate ,添加了{action}

此錯誤意味着 MVC 框架找不到您作為參數傳遞給Edit方法的id屬性的值。

MVC 在路徑數據、查詢字符串和表單值等位置搜索這些值。

例如,以下內容將在您的查詢字符串中傳遞id屬性:

/Edit?id=1

更好的方法是編輯您的路由配置,以便您可以將此值作為 URL 本身的一部分傳遞:

/Edit/1

MVC 為您的參數搜索值的這個過程稱為模型綁定,它是 MVC 的最佳功能之一。 您可以在此處找到有關模型綁定的更多信息。

表單上的操作方法是否指向/controller/edit/1

嘗試使用以下之一:

// the null in the last position is the html attributes, which you usually won't use
// on a form.  These invocations are kinda ugly
Html.BeginForm("Edit", "User", new { Id = Model.Id }, FormMethod.Post, null)

Html.BeginForm(new { action="Edit", controller="User", id = Model.Id })

或者在您的表單中添加一個隱藏的“Id”字段

@Html.HiddenFor(m => m.Id)

您收到該錯誤是因為 ASP.NET MVC 找不到為您的操作方法的 id 參數提供的 id 參數值。

您需要將其作為 url 的一部分 ("/Home/Edit/123")、作為查詢字符串參數 ("/Home/Edit?id=123") 或作為 POSTed 參數(確保具有類似於 HTML 表單中的<input type="hidden" name="id" value="123" /> )。

或者,您可以將id參數設為可為空的 int( Edit(int? id, User collection) {...} ),但如果 id 為空,您將不知道要編輯什么。

我也有同樣的問題。 我調查並發現路由模板中缺少 {action} 屬性。

代碼之前(有問題):

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

修復后(工作代碼):

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

這對於已經完成所有事情但仍面臨問題的人來說可能很有用。 對他們來說“上述錯誤也可能因引用不明確而導致”。

如果您的Controller包含

using System.Web.Mvc;

並且

using System.Web.Http;

它會產生歧義,默認情況下它將使用 MVC RouteConfig設置而不是WebApiConfig設置進行routing 確保WebAPI調用只需要System.Web.Http引用

我正面臨着同樣的錯誤。

解決方案:View 中通過其傳遞值給 Controller 的變量名稱應與 Controller 端的變量名稱匹配。

.csHtml(查看):

@Html.ActionLink("Edit", "Edit" , new { id=item.EmployeeId })

.cs(控制器方法):

 [HttpGet]
            public ActionResult Edit(int id)
            {
                EmployeeContext employeeContext = new EmployeeContext();
                Employee employee = employeeContext.Employees.Where(emp => emp.EmployeeId == id).First();
                return View(employee);
            }

使 id 參數成為可為空的 int:

public ActionResult Edit(int? id, User collection)

然后添加驗證:

if (Id == null) ...

以防萬一這對其他人有幫助; 如果您有一個視圖作為打開的選項卡,並且該選項卡取決於參數,則 Visual Studio 中可能會發生此錯誤。

關閉當前視圖並啟動您的應用程序,應用程序將“正常”啟動; 如果您打開了一個視圖,Visual Studio 會將其解釋為您想要運行當前視圖。

只需將您的代碼行更改為

<a href="~/Required/Edit?id=@item.id">Edit</a>

從您調用此函數的位置,該函數將傳遞 corect id

我有同樣的錯誤,但對我來說,問題是我使用錯誤的 GUID 執行請求。 我錯過了最后兩個字符。

360476f3-a4c8-4e1c-96d7-3c451c6c86
360476f3-a4c8-4e1c-96d7-3c451c6c865e

如果 appconfig 或 webconfig 中不存在,只需添加屬性路由

config.MapHttpAttributeRoutes()

如果您對為什么您的id通過您的 razor 文件傳遞 null 值感到困惑,我認為重載的順序是錯誤的。 在這種情況下,您不必擔心它可以為空。

例子:

這不會傳遞您的id ,因為重載不在正確的 position 中:

@Html.ActionLink("Edit", "Edit", "Controller", new { @class = "btn btn-primary" }, new { @id = x.Id })

這是傳遞id的正確順序,后面有 html 個屬性:

@Html.ActionLink("Edit", "Edit", "Controller", new { @id = x.Id }, new { @class = "btn btn-primary" })

@Html.ActionLink(item.Name, "Edit", new { id = item.Id }) 注意這里給 ActionLink() 的參數是有序的。

  1. 第一個參數用於在頁面上顯示的文本字段。
  2. 第二個用於操作 URL。
  3. 清單一供ID參考。

我有同樣的錯誤,但對我來說,問題是

我需要在索引視圖中添加以下詳細信息操作鏈接中缺少的內容

new { id = item.user_id}

這是完整的代碼:

    @foreach (var item in Model)
 {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.user_id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.user_name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.user_password)
            </td>
    
            @Html.ActionLink("Details", "Details", new { id = item.user_id}) 
               
        </tr>
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM