access child action parameter in its view

1.1k views Asked by At
public class AController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

and Index view goes like

...
@Html.Action("Index", "BController", new { HasEditPermission = true })
...

BControler goes like

public class BController : Controller
{
    public ActionResult Index()
    {
        return PartialView();
    }
}

and this Index Partial view goes like

...
@if (!string.IsNullOrEmpty(Request.Params["HasEditPermission"]) && bool.Parse(Request.Params["HasEditPermission"]))
{
 // some html rendering
}
...

here when I do Request.Params["HasEditPermission"] in the partial view, I get null.. but if I include HasEditPermission as a parameter to my Index action of BController then I get the value..

I think, I am not getting it from Request.Params because actual request is to AController Index action which doesn't include this parameter.

But I am wondering how is it binding the value to HasEditPermission parameter of Index action(suppose if I have it there) of BController?

Please could someone tell me how do I access it directly in the view? (I am aware using ViewBag in Index is an option)

1

There are 1 answers

3
Abbas Amiri On BEST ANSWER

You can use ValueProvider.GetValue("HasEditPermission").RawValue to access the value.

Controller:

public class BController : Controller
{
    public ActionResult Index()
    {
         ViewBag.HasEditPermission = Boolean.Parse(
              ValueProvider.GetValue("HasEditPermission").RawValue.ToString());

        return PartialView();
    }
}

View:

...
@if (ViewBag.HasEditPermission)
{
    // some html rendering
}
...

Update:

Request.Params gets a combined collection of QueryString, Form, Cookies, and ServerVariables items not RouteValues.

In

@Html.Action("Index", "BController", new { HasEditPermission = true })

HasEditPermission is a RouteValue.

you can also try something like this

ViewContext.RouteData.Values["HasEditPermission"]

in your View and subsequent child action views as well..