@Html.DisplayNameFor for details model

I found there was no need for helper methods, extra code or looping through the collection. I just used the following: @Html.DisplayNameFor(model => model.Carriers.FirstOrDefault().Name) This still works even if FirstOrDefault() would return null because it’s only looking for meta data, the Name property itself is not accessed. Many thanks to @Kurian who inspired this answer.

ASP.NET MVC 2 – Html.DropDownListFor confusion with ViewModel

I was also experiencing the same issue. When I debug the Action and look at the ModelState.Values[1].Errors[0].Exception for example, I see the following: {“The parameter conversion from type ‘System.String’ to type ‘System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Int64, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]’ failed because no type converter can convert between these types.”} System.Exception {System.InvalidOperationException} In my scenario, … Read more

DropDownListFor does not select value if in for loop

Not sure if this is new to mv4 or if it exists in prior version. But the DropDownListFor includes an additional parameter for the SelectList Constructor. SelectList(IEnumerable, String, String, Object) For example: Html.DropDownListFor( x => x.Countries[ i ], New SelectList(Model.CountryList,”ID”,”Description”,Model.Countries[i])) Where ID is the Country ID in the CountryList object and Description is the Country … Read more

ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses

I use HtmlHelpers all the time, most commonly to encapsulate the generation of boilerplate HTML, in case I change my mind. I’ve had such helpers as: Html.BodyId(): generates a conventional body id tag for referencing when adding custom css for a view. Html.SubmitButton(string): generates either an input[type=submit] or button[type=submit] element, depending on how I want … Read more

How to Unit Test HtmlHelper with Moq?

Here’s another article that shows you how to achieve the same thing: public static HtmlHelper CreateHtmlHelper(ViewDataDictionary vd) { var mockViewContext = new Mock<ViewContext>( new ControllerContext( new Mock<HttpContextBase>().Object, new RouteData(), new Mock<ControllerBase>().Object), new Mock<IView>().Object, vd, new TempDataDictionary()); var mockViewDataContainer = new Mock<IViewDataContainer>(); mockViewDataContainer.Setup(v => v.ViewData).Returns(vd); return new HtmlHelper(mockViewContext.Object, mockViewDataContainer.Object); }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)