MVC-wie machen Modell verwenden ViewResult im filter

Ich ein MVC-Projekt und ich will das Modell in der Ansicht von filtern.

Aber ich weiß nicht kown ,Wie kann ich dies tun.

Modell:

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(nur 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;
}

Bearbeiten beginnen danke, @Richard Szalay @Zabavsky @James @spaceman

filter Wechsel reicht, um 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 Ende

Test/Test.chtml

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

wenn ich Anfrage

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

Die Testseite kann nicht Modell.

InformationsquelleAutor der Frage zt9788 | 2013-11-18

Schreibe einen Kommentar