![](/img/trans.png)
[英]The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' . .
[英]The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32'
我正在構建我的第一個 MVC 應用程序,我在數據庫中有一個包含 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}
表單上的操作方法是否指向/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() 的參數是有序的。
我有同樣的錯誤,但對我來說,問題是
我需要在索引視圖中添加以下詳細信息操作鏈接中缺少的內容
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.