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

Fun (?) with Linq Expressions in extension methods

If you don’t need the title attribute on individual options your code could be simplified to: public static HtmlString SelectFor<TModel, TProperty, TIdProperty, TDisplayProperty, TListItem>( this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TListItem> enumeratedItems, Expression<Func<TListItem, TIdProperty>> idProperty, Expression<Func<TListItem, TDisplayProperty>> displayProperty, object htmlAttributes ) where TModel : class { var id = (idProperty.Body as MemberExpression).Member.Name; var display = … Read more

ASP.NET MVC alongside Web Forms in the same web app?

Mvc is build on top of asp.net as is webforms, so yes it’s easy. Done it couple of times for conversion purposes Maybe this url’s could help you: http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx and http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

IValidatableObject Validate method firing when DataAnnotations fails

Considerations after comments’ exchange: The consensual and expected behavior among developers is that IValidatableObject‘s method Validate() is only called if no validation attributes are triggered. In short, the expected algorithm is this (taken from the previous link): Validate property-level attributes If any validators are invalid, abort validation returning the failure(s) Validate the object-level attributes If … Read more

Asp.Net MVC3, returning success JsonResult

{“Success”:”False”,”Message”:”Error Message”} is valid JSON. You can check it here. in jsonlint.com You don’t even need a Dictionary to return that JSON. You can simply use an anonymous variable like this: public ActionResult YourActionMethodName() { var result=new { Success=”False”, Message=”Error Message”}; return Json(result, JsonRequestBehavior.AllowGet); } to Access this data from your client, you can do … Read more

Can gzip compression be selectively disabled in ASP.NET/IIS 7?

@Aristos’ answer will work for WebForms, but with his help, I’ve adapted a solution more inline with ASP.NET/MVC methodology. Create a new filter to provide the gzipping functionality: public class GzipFilter : ActionFilterAttribute { public override void OnActionExecuted(ActionExecutedContext filterContext) { base.OnActionExecuted(filterContext); var context = filterContext.HttpContext; if (filterContext.Exception == null && context.Response.Filter != null && !filterContext.ActionDescriptor.IsDefined(typeof(NoGzipAttribute), … Read more

How to extend MVC3 Label and LabelFor HTML helpers?

You can easily extend the label by creating your own LabelFor: Something like this should do what you need public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes) { return LabelFor(html, expression, new RouteValueDictionary(htmlAttributes)); } public static MvcHtmlString LabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, IDictionary<string, object> htmlAttributes) { ModelMetadata metadata = … Read more

How to use multiple form elements in ASP.NET MVC

Definitely a job for an editor template. So in your view you put this single line: @Html.EditorFor(x => x.Guests) and inside the corresponding editor template (~/Views/Shared/EditorTemplates/Guest.cshtml) @model AppName.Models.Guest <div> First Name:<br /> @Html.TextBoxFor(x => x.FirstName) </div> And that’s about all. Now the following actions will work out of the box: public ActionResult Index(int id) { … Read more

View Model IEnumerable property is coming back null (not binding) from post method?

You are not using your lambda expression properly. You need to be accessing the Products list through the model. Try doing it like this: @count = 0 foreach (var item in Model.Products) { <div> @Html.LabelFor(model => model.Products[count].ID) @Html.EditorFor(model => model.Products[count].ID) </div> <div> @Html.LabelFor(model => model.Products[count].Name) @Html.EditorFor(model => model.Products[count].Name) </div> <div> @Html.LabelFor(model => model.Products[count].Price) @Html.EditorFor(model => … Read more