我正在使用 .net MVC 创建一个演示应用程序。
以下是来自我的 StudentController 的代码片段。
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");
}
来自RouteConfig的代码片段:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
当我访问url
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
.但是当我访问url
http://localhost:54976/student/Edit?StudentId=1
时,一切正常。我对.net MVC不熟悉,请问有谁能给我建议吗?