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?
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).
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.
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With