开发者

asp.net mvc create a database record

So, I've been trying to create a record.however I created it successfully but the problem is that I may need the ID that has been auto incremented.

 [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)
        {

        } 

after this, I tried to do this which has not been working

    var id = Int32.Parse(form["CustomerServiceMappingID"]);
        var qw = (from m in dc.Mapings
                  where id == m.CustomerServiceMappingID
                  s开发者_如何学编程elect m.CustomerID).First();
      //  var id = Int32.Parse(form["CustomerID"]);
        return RedirectToAction("Index", new { id = qw });

Now I need to send Customer ID as a parameter to Index.. SO, can u help me out..

Thanks,


I would rewrite as (dont exclude the ID from the parameter list - a particular reason this needs to be excluded?):


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


0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜