25

I make a MVC project and I want set Model into View from filter.

But I do not kown ,How can I do this.

the Model:

public class TestModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}

Contorller:

[CustomFilter(View = "../Test/Test")]//<===/Test/Test.cshtml
public ActionResult Test(TestModel testModel)//<===Model from Page
{
      //the Model has Value!!
       // if has some exception here
        return View(model);//<=====/Test/Test.cshtml
}

filter(just demo):

public override void OnActionExecuting(ActionExecutingContext filterContext){
     ViewResult vr = new System.Web.Mvc.ViewResult()
     {
            ViewName = this.View,//<======/Test/Test.cshtml
            ViewData = filterContext.Controller.ViewData                             
      };
      //How can I set Model here?!!
      vr.Model = ???? //<========the Model is only get
      filterContext.Result = vr;
}

Edit begin thanks for @Richard Szalay @Zabavsky @James @spaceman

change filter extends to HandleErrorAttribute

  ViewResult vr = new System.Web.Mvc.ViewResult()
     {
            ViewName = this.View,//<======/Test/Test.cshtml
            ViewData = new ViewDataDictionary(filterContext.Controller.ViewData)
            {
                //I want get testModel from Action's paramater
                //the filter extends HandleErrorAttribute
                Model = new { ID = 3, Name = "test" }// set the model
            }                             
      };

Edit end

Test/Test.chtml

@model TestModel
<h2>Test</h2>
@Model //<=====model is null

when I request

http://localhost/Test/Test?ID=3&Name=4

The Test Page can not get Model.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
zt9788
  • 948
  • 4
  • 16
  • 31

5 Answers5

31
ViewResult vr = new System.Web.Mvc.ViewResult
    {
        ViewName = this.View, //<======/Test/Test.cshtml
        ViewData = new ViewDataDictionary(filterContext.Controller.ViewData)
            {
                Model = // set the model
            }
    };
Zabavsky
  • 13,340
  • 8
  • 54
  • 79
9

from the asp.net mvc source, they just set the model in view data. http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/Controller.cs

 protected internal virtual ViewResult View(string viewName, string masterName, object model)
    {
        if (model != null)
        {
            ViewData.Model = model;
        }

        return new ViewResult
        {
            ViewName = viewName,
            MasterName = masterName,
            ViewData = ViewData,
            TempData = TempData,
            ViewEngineCollection = ViewEngineCollection
        };
    }
spaceman
  • 1,628
  • 1
  • 16
  • 19
5

You can modify the filter context, and set the View, the Model, the ViewData and whatever you want. You have to take some things into account:

// You can specify a model, and some extra info, like ViewBag:
ViewDataDictionary viewData = new ViewDataDictionary
{
    Model = new MyViewModel
    {
        ModelProperty = ...,
        OtherModelProperty = ...,
        ...
    } 
};

// You can take into account if it's a partial or not, to return a View 
// or Partial View (casted to base, to set the remaining data):
ViewResultBase result = filterContext.IsChildAction
    ? new PartialViewResult()
    : (ViewResultBase) (new ViewResult());

// Set the remaining data: Name of the View, (if in Shared folder) or
// Relative path to the view file with extension, like "~/Views/Misc/AView.cshtml"
result.ViewName = View;                     
result.ViewData = viewData;     // as defined above

// Set this as the result
filterContext.Result = result;  

In this way, your View will receive the model, as desired.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
3

The model property is really just a ViewDataDictionary, you can initialise an instance of that with your actual model i.e.

vr.Model = new ViewDataDictionary(model);
James
  • 80,725
  • 18
  • 167
  • 237
0

You can get the controller from the filtercontext and use the View method on the controller, like so:

filterContext.Result = (filterContext.Controller as Controller).View(model);
Richard Garside
  • 87,839
  • 11
  • 80
  • 93