tags:

views:

55

answers:

1

I have a bunch of controller actions mostly used for saving data to backend storage. For now most of them use a signature like this:

    //
    // POST: /WidgetZone/Create
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)

as you can see, it accepts FormCollection. This works fine with classic user views. Now I want to JSON- enable these actions. And I do it using JsonPox action filter like this:

    //
    // POST: /WidgetZone/Create
 [JsonPox]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection collection)

Will this work when the action expects FormCollection?

For instance this work without issues (of course I construct Json object in my JavaScript client-side to pass it into this action):

    //
    // POST: /WidgetZone/Create
 [JsonPox]
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(string id, string description)

It is all about a task of converting postback UI into asynchronous one, so that saves and updates would be done async. Am I on the right track? I do think that developing separate Json, XML or classic ViewResult actions is not the best way to go.

Help appreciated

A: 

This filter is based on the the OnActionExecuted method which is run after the action method is executed in order to JSON or XML serialize the returned model. What you have as input to your action method is not important. Once the action finishes execution the filter will look for the model that you stored in the ViewResult and serialize it according to the Content-Type header that's passed in the request.

Darin Dimitrov
What about sending back the feedback? For instance, I want to return "Create successful" if there was no exception or "Create failed" if something went wrong.Within controller I do it with ViewData["Message"] = "Success" and then access ViewData in the view itself.How to return this text with Json? Should I add additional property to my model class? For instance, property LastActionResult to WidgetZone class? Then return created object Json serialized and this property would be included too. ??
mare
Yes, you need to add a property to your model as the filter will serialize only the contents of the model and not what you have in ViewData.
Darin Dimitrov