1
votes

Is it possible to pass strongly typed custom object from view to controller action method in a get or post request (not the model object which is used to bind data in the view)?. I have a view model object which has an "event type" property and then few other properties as event arguments. for every post or get request from the view, I want to create an instance of this view model object and pass the event type (event type indicates what action is done by the user as an enum and set the required properties). the object is created by the model binder, but the values are not getting populated. but if I pass anonymous object, then I can extract the values, but I need to declare a parameter for every property in the anonymous object, which I want to avoid.

sample code:

@Ajax.ActionLink("link1","ActionMethod1", new EventData {EventType="event1",Arg1=@arg1})

@Ajax.ActionLink("link2","ActionMethod1", new EventData {EventType="event2",Arg2=@arg2})

Action Method:

[HttpPost]
public void HandleEvent(EventData eventData)
{
     if (eventData != null)
     {
         //perform action
     }
}
2

2 Answers

0
votes

If I read your question correctly you are trying to submit a strongly typed object from the browser via HTTP GET/POST to the Controller and have it deserialize into the desired object?

This is absolutely possible. In the browser just serialize into a JSON object that matches your server-side type.

Checkout the Form plugin http://jquery.malsup.com/form/ which will do this for you.

0
votes

Have you tried wrapping your strongly typed object in a generic object?

@Ajax.ActionLink("link1","ActionMethod1", new{eventData = new EventData {EventType="event1",Arg1=@arg1}})

I'm not sure if this will give you the expected results or not, but I think its worth a try.