Every example that I can find of an MVC4 app has the edit working on one row of data at a time. It displays all the rows of data with each row having an edit which takes you to another page and allows you to edit that one row.
What I would like to do is display all the data elements in rows and instead of having the user have to click EDIT on each row, all the rows’ data points would already be in text boxes which the user can directly update. And there is just one SAVE on the page that would just save all the updates/edits at once.
How can I setup my MVC app to support that?
You can use an EditorTemplates for this. The below example shows the normal form posting example. You can ajaxify it if you need by using the
serializemethod and sending form values.Assuming You need to Edit the List of Student Names for a course. So Let’s create some viewmodels for that
Now in your
GETaction method, you create an object of our view model, initialize theStudentscollection and send it to our strongly typed view.Now Create a folder called EditorTemplates under Views/YourControllerName. Then create a new view under that called
Student.cshtmlwith below contentNow in our main view (StudentList.cshtml), Use EditorTemplate HTML helper method to bring this view.
This will bring all the UI with each of your student name in a text box contained in a table row. Now when the form is posted, MVC model binding will have all text box value in the
Studentsproperty of our viewmodel.Ajaxified solution
If you want to Ajaxify this, you can listen for the submit button click, get the form and serialize it and send to the same post action method. Instead of redirecting after saving, you can return some JSON which indicates the status of the operation.