I am using a create form with asp.net mvc 3, where a user enters information and submits, to save their typed information to a database. I would like to display to the user the Id generated with the database row they have just created (via a view for form submitted successfully your id number is…). Does anyone know of a method to accomplish this?
Share
Your question needs a bit more detail however there are a few things one should consider when coming up with a data persistence strategy. If you’re using the Asp.Net Membership provider, you need to specify that as well. There are methods for returning user info as Bildr pointed out, but I don’t recommend using that provider unless you need to as it is being replaced with a ‘Simple Membership Provider’ in MVC4.
If your using a repository pattern, you could have classes inherit from a common interface. When you add an entity of type T it returns that entity back to the user, when you perform a save it doesn’t. This approach comes in handy when you’re crossing boundaries, such as with WCF Services and you are passing in a complex object graph with multiple child objects. You can of course, be a little more nimble and have your save method simply return the ID of the created object.
Of course, if you’re building a simpler application, and you are as over repositories as Jimmy Bogard is, and using an ORM, such as EntityFramework then you would pass in your object (again, it’s by reference) to your context object:
If you clarify your question, I’d be happy to provide a more specifics. Good luck!