1

I am trying to use ViewBag inside quotes in ASP.NET like so:

@using (Html.BeginForm("Index?community=" + @ViewBag.community + "&lot=" + @ViewBag.lot + "",
                        "UploadFile",
                        FormMethod.Post,
                        new { enctype = "multipart/form-data" }))
            {
                <label for="file">Upload File:</label>
                <input type="file" name="file" id="file" /><br><br>
                <input type="submit" value="Upload File" />
                <br><br>
                @ViewBag.Message
            }

I have also tried the following:

@using (Html.BeginForm("Index?community=@ViewBag.community&lot=@ViewBag.lot",
                        "UploadFile",
                        FormMethod.Post,
                        new { enctype = "multipart/form-data" }))
            {
                <label for="file">Upload File:</label>
                <input type="file" name="file" id="file" /><br><br>
                <input type="submit" value="Upload File" />
                <br><br>
                @ViewBag.Message
            }

Is it possible to use @ViewBag inside quotes ?

user990423
  • 1,397
  • 2
  • 12
  • 32
user979331
  • 11,039
  • 73
  • 223
  • 418
  • Also, you might find your life makes more sense with strongly typed models. Suggest using custom view models instead of the messy ViewBag. https://stackoverflow.com/questions/13779294/viewmodels-or-viewbag – Todd Sprang Oct 20 '15 at 15:06

2 Answers2

2

There's nothing special about ViewBag, it's a property available in the context of the page like any other. Just refer to it when building the string like you would in any other C# code:

"Index?community=" + ViewBag.community
David
  • 208,112
  • 36
  • 198
  • 279
  • I get this error: Error 1 'System.Web.Mvc.HtmlHelper' has no applicable method named 'BeginForm' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. – user979331 Oct 20 '15 at 15:18
  • @user979331: A couple of things may cause that. It might not like the fact that `ViewBag` is `dynamic`. Try: `((string)ViewBad.community)` instead? – David Oct 20 '15 at 15:22
  • I am bit confused. can you give me an example on how it should look? – user979331 Oct 20 '15 at 15:30
2

Sure: just remove the @ since you already defined you're writing an expression at the beginning of the line (@using). Do this:

@using (Html.BeginForm("Index?community=" + ((string)ViewBag.community) + "&lot=" + ((string)ViewBag.lot),[...]

Anyway I suggest to use a view model to pass values from controller to view, instead of ViewBag.

Alberto
  • 1,853
  • 1
  • 18
  • 22
  • I get this error: Error 1 'System.Web.Mvc.HtmlHelper' has no applicable method named 'BeginForm' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. – user979331 Oct 20 '15 at 15:18
  • I just reported your example to show how you can use the ViewBag. I didn't test the whole code. However do this: @using (Html.BeginForm("Index?community=" + ((string)ViewBag.community) + "&lot=" + ((string)ViewBag.lot), [...] – Alberto Oct 21 '15 at 14:31
  • Edited answer with updated code and a general tip ;) – Alberto Oct 21 '15 at 14:37