开发者

Fails to execute index action

i have a problem in MVC ASP.NET project. i work with LINQ technology.When i want to insert one record to table,this error is shown.although insert action is preformed:**

ERROR:

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.

controller methods for insert(create):

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();
    }
}


Your controller is expecting an int named id, and not getting one. If the integer is nullable, then you should use an int? parameter instead.

Instead of

public ActionResult Index(int id) {
}

you should use

public ActionResult Index(int? id) {
}

instead.

If the id should be there and you're use a PRG pattern, be sure to include the id when redirecting after the form submission by using return RedirectToAction("Index", new { id = 1 }); (only replace the 1 with the real value).


Try this:

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

Replace the controller, action and id by yourself. If the error still occurs, please check your session's value.


The problem is probably in the controller method that is called after the insert is performed. The method is expecting an int id parameter, but the redirect that is routed to the method isn't passing an id value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜