The name ‘ViewBag’ does not exist in the current context

Sounds like you’re missing the following in the Web.Config in the views folder: /Views/Web.Config <?xml version=”1.0″?> <configuration> <system.web.webPages.razor> <host factoryType=”System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ /> <pages pageBaseType=”System.Web.Mvc.WebViewPage”> // <– this line and contents are important <namespaces> <add namespace=”System.Web.Mvc” /> <add namespace=”System.Web.Mvc.Ajax” /> <add namespace=”System.Web.Mvc.Html” /> <add namespace=”System.Web.Routing” /> </namespaces> </pages> </system.web.webPages.razor> Views typically derive from … Read more

ASP.NET MVC 3 JSONP: Does this work with JsonValueProviderFactory?

As far as receiving a JSON string and binding it to a model is concerned the JsonValueProviderFactory does this job out of the box in ASP.NET MVC 3. But there is nothing built-in for outputting JSONP. You could write a custom JsonpResult: public class JsonpResult : JsonResult { public override void ExecuteResult(ControllerContext context) { if … Read more

Loading a partial view in jquery.dialog

Try something like this: <script type=”text/javascript”> $(function () { $(‘#dialog’).dialog({ autoOpen: false, width: 400, resizable: false, title: ‘hi there’, modal: true, open: function(event, ui) { //Load the CreateAlbumPartial action which will return // the partial view _CreateAlbumPartial $(this).load(“@Url.Action(“CreateAlbumPartial”)”); }, buttons: { “Close”: function () { $(this).dialog(“close”); } } }); $(‘#my-button’).click(function () { $(‘#dialog’).dialog(‘open’); }); }); … Read more

Posting to a list MVC3

Replace the following loop: @foreach (var item in Model.Entries) { <tr> <td> @Html.EditorFor(x => item.Repetition) </td> <td> @Html.EditorFor(x => item.Weight) </td> </tr> } with: @for (var i = 0; i < Model.Entries.Count; i++) { <tr> <td> @Html.EditorFor(x => x.Entries[i].Repetition) </td> <td> @Html.EditorFor(x => x.Entries[i].Weight) </td> </tr> } or even better, use editor templates and replace … Read more

DropdownListFor default value

So, I did something like this: @Html.DropDownListFor(model => model.Dessert, new SelectList(Model.AvailableDesserts, “DessertID”, “DessertName”), “—Select A Dessert —“) Seems to work pretty well. Dessert in my viewmodel is the one selected by the user. AvailableDesserts is a collection of ones to pick from. Hope that helps.