Tuesday, February 19, 2013

Sending Data from View to Controller to Another View Back to Controller

I faced another interesting  coding challenge today. It was a challenge for me, since I've only had nine months of project experience so far in .NET; with MVC3 I sort of had to wing it for the past five months. We had a View page that consisted of a form and a table; inside the form were two dropdown lists that got dynamically filled based on the returned data from the db call. The table had a column that featured ActionLinks to a detail page providing more information. Inside the detail page was a Back link that when clicked is supposed to return you to the original page with the form and table, but the catch was the original information being displayed (for example, if you changed the selected values in the dropdown lists) should be retained. This was important for usability so that the people who would be making use of the module don't have to repeatedly choose the same settings for the dropdown lists if they need to do work with a particular configuration.

The table and form submission were already taken care of by a separate jquery file. It might have been a better idea if the details page had been made into a modal window or a partial view, but that wasn't what was done and redoing the code would have been a headache. As always, I did what in my opinion amounts to a patch job. It's not ideal, but it required minimal modification of code and at my current level I think it's better not to screw up.

For convenience, let's call one dropdown the name dropdown list and the other, the status dropdown list. The original page and the detail page being separate views that display different information, they have their own models. The GET method for the original page took an instance of the page's model class as an argument, while the detail page's GET method took the id of the row on the table that was clicked as an argument. The information that displays on the table in the original page is changed when the selected value in the dropdown lists change. I only needed to change the selected values following clicking the Back link in the detail page, as that hyperlink already went to the original page anyway. To do that, I needed somehow to send the current form configuration through two views and the Controller twice.

I did this by making sure both models of the original and detail pages had selectedName and selectedStatus properties. The model for the original page already had this, so I just modified the model for the detail page. In the section of the code for the original page's view that looped in order to fill up the table, I added the selectedName and selectedStatus values like so:


                         @Html.ActionLink(item.Code, "controllerGetMethodForDetails",
                          "controllerName", new {
                             id = item.Id,
                             selectedName = @Model.SelectedName,
                             selectedStatus = @Model.SelectedStatus                                  
                         }, null)

I added the two strings to the detail page's GET method's arguments. For the Back hyperlink, I used this code:

@Html.ActionLink("Back", "controllerGetMethodForOriginal", new {
                       selectedName = @Model.Name,
                       selectedStatus = @Model.Status
}, new AjaxOptions { HttpMethod = "POST" })

The surprising thing was that somehow the system already knew that these values I am passing with the hyperlink were supposed to correlate with the properties in the original page's Model. I did not know that and I felt a little proud of myself for a few minutes for discovering it. It's stuff like this that keeps me from getting bored as a software engineer.

No comments:

Post a Comment