我在.net MVC中創建一個演示應用程序。可選參數必須是引用類型,可爲空類型或聲明爲可選參數。參數名稱:參數`
下面是我的StudentController的代碼片段。從RouteConfig
public ActionResult Edit(int studentId)
{
var std = studentList.Where(s => s.StudentId == studentId).FirstOrDefault();
return View(std);
}
[HttpPost]
public ActionResult Edit(Student std)
{
//write code to update student
return RedirectToAction("Index");
}
代碼片段:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
當我打的網址http://localhost:54977/student/Edit/1
我得到以下異常。
The parameters dictionary contains a null entry for parameter 'studentId' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'MVC1.Controllers.StudentController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters
。
但它工作正常,當我打網址http://localhost:54976/student/Edit?StudentId=1
。
我是.net MVC的新手。任何人都可以在這個建議我。
顯示您的RouteMap設置請 –
我在我的問題中添加了相同的內容.. –