2

I've got a snippet of code in a custom ActionResult

VB.NET Version

Dim result As New ViewResult()
result.Model= data   ## Property Model is ReadOnly
Return result

C# Version

ViewResult result = new ViewResult();
result.Model = data;  // Property Model is ReadOnly
return result;

How do I properly return a View from within a custom ActionResult that can also include the model?

Chase Florell
  • 46,378
  • 57
  • 186
  • 376
  • Are you trying to programmatically execute a view? – Buildstarted Oct 03 '11 at 04:55
  • Yup. Essentially I need to create a custom ActionResult (I'm calling it `Restful` whereby it checks the Request.ContentType and returns the appropriate format (Html, Json, Xml) – Chase Florell Oct 03 '11 at 04:57
  • Ah, that's not really supported. It's possible but not very easy. I have an answer somewhere that will get you in the right direction. Check this post out: http://stackoverflow.com/questions/3808076/using-the-razor-view-engine-in-a-different-way/3815216#3815216 – Buildstarted Oct 03 '11 at 06:01

1 Answers1

3

Setting

result.ViewData.Model = data; 

Should help you. In fact, get ViewResultBase.Model is implemented as

public object Model 
{
   get {
          return ViewData.Model;
       }
}
archil
  • 39,013
  • 7
  • 65
  • 82