0

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
                  select 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,

1
  • serviceToCreate.Id will be set after you do db.SubmitChanges(). Unless of course an error occured, which you'll never know because your swallowing it. Commented Jun 10, 2011 at 3:42

1 Answer 1

1

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


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.