How to use a ViewBag to create a dropdownlist?

You cannot used the Helper @Html.DropdownListFor, because the first parameter was not correct, change your helper to: @Html.DropDownList(“accountid”, new SelectList(ViewBag.Accounts, “AccountID”, “AccountName”)) @Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns. Here’s the documentation If your View it’s strongly typed to some Model you … Read more

How to create own dynamic type or dynamic object in C#?

dynamic MyDynamic = new System.Dynamic.ExpandoObject(); MyDynamic.A = “A”; MyDynamic.B = “B”; MyDynamic.C = “C”; MyDynamic.Number = 12; MyDynamic.MyMethod = new Func<int>(() => { return 55; }); Console.WriteLine(MyDynamic.MyMethod()); Read more about ExpandoObject class and for more samples: Represents an object whose members can be dynamically added and removed at run time.

Updating PartialView mvc 4

So, say you have your View with PartialView, which have to be updated by button click: <div class=”target”> @{ Html.RenderAction(“UpdatePoints”);} </div> <input class=”button” value=”update” /> There are some ways to do it. For example you may use jQuery: <script type=”text/javascript”> $(function(){ $(‘.button’).on(“click”, function(){ $.post(‘@Url.Action(“PostActionToUpdatePoints”, “Home”)’).always(function(){ $(‘.target’).load(‘/Home/UpdatePoints’); }) }); }); </script> PostActionToUpdatePoints is your Action with … Read more

ViewModels or ViewBag?

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Everything should be done inside a view model. That’s what a view model is. A class that you specifically define to meet the requirements of your view. Don’t mix ViewBags with ViewModels. It is no longer clear for the … Read more

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

I was having the same problem. Turned out I was missing the ./Views/Web.config file, because I created the project from an empty ASP.NET application instead of using an ASP.NET MVC template. For ASP.NET MVC 5, a vanilla ./Views/Web.config file contains the following: <?xml version=”1.0″?> <!– https://stackoverflow.com/a/19899269/178082 –> <configuration> <configSections> <sectionGroup name=”system.web.webPages.razor” type=”System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, … Read more

How ViewBag in ASP.NET MVC works

ViewBag is of type dynamic but, is internally an System.Dynamic.ExpandoObject() It is declared like this: dynamic ViewBag = new System.Dynamic.ExpandoObject(); which is why you can do : ViewBag.Foo = “Bar”; A Sample Expander Object Code: public class ExpanderObject : DynamicObject, IDynamicMetaObjectProvider { public Dictionary<string, object> objectDictionary; public ExpanderObject() { objectDictionary = new Dictionary<string, object>(); } … Read more