Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass an XDocument as a parameter to an action in ASP.NET MVC?

I am wondering if it is possible to write a controller action in ASP.NET MVC that takes as a parameter an XDocument. This would of course just mean that the form post would send a string of XML.

Is there anything special that I would need to do to accept this as a parameter?

like image 489
Brendan Enrick Avatar asked May 19 '11 18:05

Brendan Enrick


People also ask

How do you pass an object from one action to another action in MVC?

You can use TempData to pass the object. This worked well, Thanks Eranga. One potential pitfall is that the object can be disposed before it gets used in the view if you have a overridden definition for Dispose in your controller(something VS tends to add in with its auto generated CRUD code).

How you can send a data from action method to view?

ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.


1 Answers

You could write a custom type binder and register it in the Application Start event handler in global.asax:

protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(XDocument), new YourXDocumentBinder());
}

The MVC pipeline would automatically call the binder when it encountered an action with a XDocument argument.

The binder implementation would look something like this:

public class YourXDocumentBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
         // handle the posted data
    }
}
like image 69
m0sa Avatar answered Sep 20 '22 13:09

m0sa