[英]Cant routing well in MVC3 ASP.NET c#
我正在構建我的第一個MVC應用程序,正在使用:
1)Visual Studio 2010旗艦版。 2)Mysql數據庫3)EntityFramework 4
這是我的代碼
模型:
namespace MvcApplication2.Models{
using System;
using System.Collections.Generic;
public partial class region
{
public region()
{
this.provincia = new HashSet<provincia>();
}
public int REGION_ID { get; set; }
public string REGION_NOMBRE { get; set; }
public virtual ICollection<provincia> provincia { get; set; }
}}
控制器:
//
// GET: /Region/Edit/5
public ActionResult Edit(int id)
{
region region = db.region.Find(id);
return View(region);
}
//
// POST: /Region/Edit/5
[HttpPost]
public ActionResult Edit(region region)
{
if (ModelState.IsValid)
{
db.Entry(region).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(region);
}
的Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
當我嘗試編輯Region的某行時,這是錯誤。
參數字典包含>'MvcApplication2.Controllers.RegionController'中方法'System.Web.Mvc.ActionResult Edit(Int32)'的非空類型>'System.Int32'的參數'id'的空條目。 可選參數必須是引用類型,>可以為null的類型,或者必須聲明為可選參數。 參數名稱:參數
當我打電話給編輯時,請將我發送到/ Region / Edit,它應該是/ Region / Edit / some_id
提前致謝
嘗試分析在其中構造鏈接的cshtml
頁面。
它應包含以下區域的ID:
@Html.ActionLink("Edit", "Edit", new { id = item.REGION_ID })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.