Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing ASP.NET MVC SubControllers with action parameters?
    primarykey
    data
    text
    <p>I am experimenting with <a href="http://mhinze.com/subcontrollers-in-aspnet-mvc/" rel="nofollow noreferrer">MvcContrib subcontrollers</a>. Looking at the example in the source, your parent controller (HomeController) takes an action which takes the subcontroller (FirstLevelSubController) as a parameter:</p> <pre><code>public class HomeController : Controller { public ActionResult Index(FirstLevelSubController firstLevel) { ViewData["Title"] = "Home Page"; return View(); } } </code></pre> <p>In Home's index view, you call ViewData.Get like this to render the subcontroller and it's view:</p> <pre><code>&lt;div style="border:dotted 1px blue"&gt; &lt;%=ViewData["text"] %&gt; &lt;% ViewData.Get&lt;Action&gt;("firstLevel").Invoke(); %&gt; &lt;/div&gt; </code></pre> <p>The subcontroller's action gets called (ignore the secondlevelcontroller, the example is just demonstrating how you can nest multiple subcontrollers):</p> <pre><code>public class FirstLevelSubController : SubController { public ViewResult FirstLevel(SecondLevelSubController secondLevel) { ViewData["text"] = "I am a first level controller"; return View(); } } </code></pre> <p>This all works, the subcontroller's view gets rendered inside the parent view.</p> <p>But what if I need <strong>other parameters</strong> in my home controller's action? For example, I may want to pass a Guid to my controller's index method:</p> <pre><code>public class HomeController : Controller { public ActionResult Index(Guid someId, FirstLevelSubController firstLevel) { //Do something with someId ViewData["Title"] = "Home Page"; return View(); } } </code></pre> <p>There doesn't seem to be any way to do &lt;% ViewData.Get("firstLevel").Invoke(); %> with parameters. So I can't figure out how to link to my controller from another controller passing a parameter like this:</p> <pre><code>Html.ActionLink&lt;HomeController&gt;(x =&gt; x.Index(someThing.Id), "Edit") </code></pre> <p>Perhaps I am approaching this the wrong way? How else can I get my parent controller to use a subcontroller, but also do interesting stuff like accept parameters / arguments?</p>
    singulars
    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.
 

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