无法执行索引操作

时间:2010-12-11 11:14:46

标签: asp.net-mvc

我在MVC ASP.NET项目中遇到问题。 我使用LINQ技术。当我想向表中插入一条记录时,会显示此错误。尽管执行了插入操作:**

ERROR:

  '/'应用程序中的服务器错误。该   parameters字典包含null   参数'id'的条目   非可空类型'System.Int32'   方法'System.Web.Mvc.ActionResult   索引(Int32)'   'MVCJahan_oil.Controllers.getehController'。   可选参数必须是a   引用类型,可空类型或者是   声明为可选参数。   参数名称:参数   描述:未处理的异常   在执行期间发生   当前的网络请求。请查看   堆栈跟踪以获取更多信息   错误及其来源   代码。

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

3 个答案:

答案 0 :(得分:2)

您的控制器期待名为int的{​​{1}},而不是一个。如果整数可以为空,那么您应该使用id参数。

而不是

int?

你应该使用

public ActionResult Index(int id) {
}

代替。

如果id应该在那里并且您使用的是PRG模式,请确保在使用public ActionResult Index(int? id) { } 提交表单后重定向时包含id(仅将1替换为实际值)。

答案 1 :(得分:1)

试试这个:

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

自行更换控制器,操作和ID。 如果错误仍然存​​在,请检查会话的值。

答案 2 :(得分:0)

问题可能在执行插入后调用的控制器方法中。该方法需要int id参数,但路由到该方法的重定向不会传递id值。

相关问题