Rob Bowman
Rob Bowman

Reputation: 8751

@Html.BeginForm with a variable controller name

I have a shared partial view from which I need to call a controller action but won't know which controller until runtime.

In each of my controller I assign the name to the ViewBag but the following won't compile.

@using (Html.BeginForm("Index", ViewBag.CurrentController))

How can I work around this?

Upvotes: 1

Views: 895

Answers (1)

NikolaiDante
NikolaiDante

Reputation: 18649

Add a cast to string on to the ViewBag call:

@using (Html.BeginForm("Index", (string)ViewBag.CurrentController))

The overload your trying to match expects a string as the controller name, and ViewBag isn't strongly typed.

Upvotes: 2

Related Questions