Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC how to make Model use ViewResult in filter
    primarykey
    data
    text
    <p>I make a MVC project and I want set Model into View from filter.</p> <p>But I do not kown ,How can I do this.</p> <p>the Model:</p> <pre><code>public class TestModel { public int ID { get; set; } public string Name { get; set; } } </code></pre> <p>Contorller:</p> <pre><code>[CustomFilter(View = "../Test/Test")]//&lt;===/Test/Test.cshtml public ActionResult Test(TestModel testModel)//&lt;===Model from Page { //the Model has Value!! // if has some exception here return View(model);//&lt;=====/Test/Test.cshtml } </code></pre> <p>filter(just demo):</p> <pre><code>public override void OnActionExecuting(ActionExecutingContext filterContext){ ViewResult vr = new System.Web.Mvc.ViewResult() { ViewName = this.View,//&lt;======/Test/Test.cshtml ViewData = filterContext.Controller.ViewData }; //How can I set Model here?!! vr.Model = ???? //&lt;========the Model is only get filterContext.Result = vr; } </code></pre> <p><strong>Edit begin</strong> thanks for @Richard Szalay @Zabavsky @James @spaceman</p> <p>change filter extends to HandleErrorAttribute</p> <pre><code> ViewResult vr = new System.Web.Mvc.ViewResult() { ViewName = this.View,//&lt;======/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 } }; </code></pre> <p><strong>Edit end</strong></p> <p>Test/Test.chtml</p> <pre><code>@model TestModel &lt;h2&gt;Test&lt;/h2&gt; @Model //&lt;=====model is null </code></pre> <p>when I request </p> <pre><code>http://localhost/Test/Test?ID=3&amp;Name=4 </code></pre> <p>The Test Page can not get Model.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload