asp.net mvc创建数据库记录

时间:2011-06-10 03:38:27

标签: asp.net asp.net-mvc-3

所以,我一直在尝试创建一个记录。但是我成功创建了它,但问题是我可能需要自动增加的ID。

 [AcceptVerbs(HttpVerbs.Post)]

 public ActionResult Create([Bind(Exclude="CustomerServiceMappingID")] Maping serviceToCreate, FormCollection form)
    {


        if (!ModelState.IsValid)

                return View();

       var dc = new ServicesDataContext();
       dc.Mapings.InsertOnSubmit(serviceToCreate);
        try
        {
            dc.SubmitChanges();
        }
        catch (Exception e)
        {

        } 

在此之后,我试图做到这个没有工作的

    var id = Int32.Parse(form["CustomerServiceMappingID"]);
        var qw = (from m in dc.Mapings
                  where id == m.CustomerServiceMappingID
                  select m.CustomerID).First();
      //  var id = Int32.Parse(form["CustomerID"]);
        return RedirectToAction("Index", new { id = qw });

现在我需要将客户ID作为参数发送给Index ..所以,你可以帮帮我..

谢谢,

1 个答案:

答案 0 :(得分:1)

我会重写为(不要从参数列表中排除ID - 这是需要排除的特殊原因吗?):


[HttpPost]
 public ActionResult Create(Maping serviceToCreate)
{
        if (!ModelState.IsValid)
        {
                return View();
        }

       var dc = new ServicesDataContext();
       dc.Mapings.InsertOnSubmit(serviceToCreate);
       dc.SubmitChanges();

      //try to get the values from 'Maping' model if possible?
       var qw = (from m in dc.Mapings
                  where m.CustomerServiceMappingID == serviceToCreate.CustomerServiceMappingId
                  select m.CustomerID).First();
       return RedirectToAction("Index", new { id = qw });