I am building an ajax form using the MVC Ajax Helpers: http://msdn.microsoft.com/en-us/library/dd505013.aspx
The code looks similar to:
Ajax.BeginForm("ActionName", "ControllerName",
new {
SomeProperty = (ViewData["SomeObject"] as SomeClass).SomeProperty,
AnotherProperty = (ViewData["SomeObject"] as SomeClass).AnotherProperty,
AllTheProperties = (ViewData["SomeObject"] as SomeClass).AllTheProperties,
otherProperty= ViewData["otherObject"]
},
new AjaxOptions {},
new {id = "anHtmlId"} );
but there are plenty of more properties in ViewData["SomeObject"] though.
I would like to add ViewData["otherObject"] to the routeData object simply with as few lines as possible and do not want have all of the properties of someObject laid out individually so that I can add properties to it without having to revisit this code in the view.
Essentially “merging” these objects to pass as the routeData parameter would be useful.
Update: an idea:
Ajax.BeginForm("ActionName", "ControllerName",
ViewData["SomeObject"].addThePropertiesOf(ViewData["otherObject"]),
new AjaxOptions {},
new {id = "anHtmlId"} );
Where addThePropertiesOf() returns a new merged object. I wouldn’t like to use the expensive bits of reflection otherwise this doesn’t seem performant.
What’s the point of sending some values to the HTML which cannot be modified by the user as they are not part of the form as input elements just to resend them back on postback as query string parameters? Makes no sense. It’s a total waste of bandwidth. For me all you need to send to the server when you post this form is some unique identifier (in addition of course to all the form fields that the user can modify):
And of course for all values that can be modified you will have corresponding input fields whose values would obviously be automatically posted to the server.
Finally on the server use this unique identifier to refetch the original values from wherever you fetched them in your GET action when you rendered the form. You fetched them from somewhere, right, otherwise you wouldn’t have been able to store them in the
ViewDatain the first place.And please get rid of those
ViewData/ViewBag, define view models and use strongly typed views. I am starting to exhibit some real allergies towards those two weakly typed constructs that should never be used in any ASP.NET MVC application.