Best practice for ASP.NET MVC resource files

You should avoid App_GlobalResources and App_LocalResources. Like Craig mentioned, there are problems with App_GlobalResources/App_LocalResources because you can’t access them outside of the ASP.NET runtime. A good example of how this would be problematic is when you’re unit testing your app. K. Scott Allen blogged about this a while ago. He does a good job of … Read more

Mapping Lists using Automapper

Mapper.CreateMap<Person, PersonViewModel>(); peopleVM = Mapper.Map<List<Person>, List<PersonViewModel>>(people); Mapper.AssertConfigurationIsValid(); From Getting Started: How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type’s design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up … Read more

Steve Sanderson’s BeginCollectionItem helper won’t bind correctly

Ok I think I see what is going on here. In the second sample, where you did the foreach, it looks like your cshtml was something like this (@ symbols may be incorrect): foreach (var war in Model.WarrantyFeaturesVm) { using (Html.BeginCollectionItem(“WarrantyFeaturesVm”)) { Html.HiddenFor(m => war.FeatureId) <span>@Html.DisplayFor(m => war.Name)</span> Html.HiddenFor(m => war.HasFeature) } } Because BeginCollectionItem … Read more

Razor Nested WebGrid

Excuse the verbose data setup but this works… @{ var data = Enumerable.Range(0, 10).Select(i => new { Index = i, SubItems = new object[] { new { A = “A” + i, B = “B” + (i * i) } } }).ToArray(); WebGrid topGrid = new WebGrid(data); } @topGrid.GetHtml(columns: topGrid.Columns( topGrid.Column(“Index”), topGrid.Column(“SubItems”, format: (item) => … Read more

show only the date in @Html.EditorFor helper

You need to use the ISO format when using type=”date” [DataType(DataType.Date, ErrorMessage=”Date only”)] [DisplayFormat(DataFormatString = “{0:yyyy-MM-dd}”, ApplyFormatInEditMode = true)] public DateTime? YearBought { get; set; } This will display the date in the browsers culture. Note there is no need to add @type = “date”. The EditorFor() method will add that because of the DataType … Read more

Inject a dependency into a custom model binder and using InRequestScope using Ninject

The ModelBinders are reused by MVC for multiple requests. This means they have a longer lifecycle than request scope and therefore aren’t allowed to depend on objects with the shorter request scope life cycle. Use a Factory instead to create the IPostRepository for every execution of BindModel

How do I populate a dropdownlist with enum values?

I’m using a helper that i found here to populate my SelectLists with a generic enum type, i did a little modification to add the selected value though, here’s how it looks like : public static SelectList ToSelectList<T>(this T enumeration, string selected) { var source = Enum.GetValues(typeof(T)); var items = new Dictionary<object, string>(); var displayAttributeType … Read more

Load PartialView with using jquery Ajax?

You could subscribe to the .change() event of the dropdown and then trigger an AJAX request: <script type=”text/javascript”> $(function() { $(‘#meters’).change(function() { var meterId = $(this).val(); if (meterId && meterId != ”) { $.ajax({ url: ‘@Url.Action(“MeterInfoPartial”)’, type: ‘GET’, cache: false, data: { meter_id: meterId } }).done(function(result) { $(‘#container’).html(result); }); } }); }); </script> and then … Read more

Maintain state of a dynamic list of checkboxes in ASP.NET MVC

I got it working after much playing around with the various different approaches. In the view: <%string[] PostFeatures = Request.Form.GetValues(“Features”);%> <% foreach (var Feature in (ViewData[“AllPropertyFeatures”] as IEnumerable<MySolution.Models.PropertyFeature>)) { %> <input type=”checkbox” name=”Features” id=”Feature<%=Feature.PropertyFeatureID.ToString()%>” value=”<%=Feature.PropertyFeatureID%>” <%if(PostFeatures!=null) { if(PostFeatures.Contains(Feature.PropertyFeatureID.ToString())) { Response.Write(“checked=\”checked\””); } } %> /> <label for=”Feature<%=Feature.PropertyFeatureID%>”> <%=Feature.Description%></label> <% } %> In the receiving controller method: … Read more