I currently learning ASP.NET MVC . I wondered if it is possible when a form is submitted to the get the id of the new record and then redirect to a new page with the new record id?
The information is entered into the database correctly, but I’m not sure how to get the id?
For example, complete a new customer details form and then redirect the page to an orders form where new orders can be added for the customer.
How are you accessing the database? If you are using an ORM (such as NHibernate or LINQ to SQL) then the object should have it’s ID property updated after you insert it into the database.
If you are hand rolling the SQL then you need to do a select in the same statement as the insert of:
This will return the ID of the row you have just created.
Then you can do this within your action:
With the ID retrieved either from the saved object or the return value of the SQL. Whichever fits your scenario.
Edit
As you’ve mentioned you are using NHibernate then you can do this: