2010-12-11 56 views
1

我在MVC ASP.NET項目中遇到問題。 我使用LINQ technology.When工作,我想插入一條記錄表,這個錯誤是shown.although插入動作預製:**未能執行索引操作

錯誤:

Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Index(Int32)' in 'MVCJahan_oil.Controllers.getehController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

用於插入控制器方法(創建) :

public ActionResult Index(int id) 
{ 
    var query= from gete in l_class.getehs where gete.request_no == id select gete; 
    this.Session["req_no"] = id; 
    return View(query.ToList()); 
} 


[HttpPost] 
public ActionResult Create([Bind(Exclude = "id")]geteh gte) 
{ 
    try 
    { 

     l_class.getehs.InsertOnSubmit(gte); 
     l_class.SubmitChanges(); 
     return RedirectToAction("Index", new { id = System.Convert.ToInt32 (Session["req_no"].ToString()) }); 
    } 
    catch 
    { 
     return View(); 
    } 
} 
+0

您可能需要更新您的問題的標題是有點更具描述性的...... – James 2010-12-11 11:52:34

回答

0

問題可能出在執行插入操作後調用的控制器方法中。該方法期望int id參數,但路由到方法的重定向不會傳遞一個id值。

2

你的控制器期待一個int命名id,並沒有得到一個。如果整數是可空的,則應該使用int?參數。

而不是

public ActionResult Index(int id) { 
} 

你應該使用

public ActionResult Index(int? id) { 
} 

代替。

如果id應該在那裏,並且您使用的是PRG模式,請務必在使用return RedirectToAction("Index", new { id = 1 });(僅將實際值替換爲1)將表單提交後重定向時包含該id。

+0

謝謝。但我的問題沒有解決。請回答我注意我的問題的變化。 – Bahareh 2010-12-11 11:41:47

+0

只是爲了增加這個問題,你的問題是你的* Index *方法需要一個不可爲空的參數(id),並且你沒有將它設置爲@Jarret正確指出的。當您轉到網址時MySite/MyController它將調用MyController中的Index操作,在您的情況下,它將通過上述異常調用,因爲它需要**整型參數。您需要:a)將索引操作中的id參數更改爲空或者b)始終在URL中傳遞一個id MySite/MyController/MyID – James 2010-12-11 12:00:17

+0

thanks.But必須完成什麼? – Bahareh 2010-12-11 12:06:55

1

試試這個:

RedirectToAction(new {controller="Home", action="Index", id=123}); 

自行更換控制器,動作和id。 如果錯誤仍然存​​在,請檢查您的會話的值。