ASP.NET MVC 3: DataAnnotations.FileExtensionsAttribute not working

Since System.ComponentModel.DataAnnotations.FileExtensionsAttribute is sealed. I use a wrapper for MVC 4. public class HttpPostedFileExtensionsAttribute : DataTypeAttribute, IClientValidatable { private readonly FileExtensionsAttribute _innerAttribute = new FileExtensionsAttribute(); /// <summary> /// Initializes a new instance of the <see cref=”HttpPostedFileExtensionsAttribute” /> class. /// </summary> public HttpPostedFileExtensionsAttribute() : base(DataType.Upload) { ErrorMessage = _innerAttribute.ErrorMessage; } /// <summary> /// Gets or sets … Read more

When do I use View Models, Partials, Templates and handle child bindings with MVC 3

When should I use View Models? Is it not recommended to use the domain? I find that my view models are copies of my domain objects, and don’t see the value… View models should always be used. You should not use your domain models in the view. View models are not exact copies of the … 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

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

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

Is it possible to display raw Html from database in ASP.NET MVC 3?

All you need is: @Html.Raw(yourEncodedHtmlFromYouDatabase) I’m assuming that the html in the database has been properly sanitized (or at least from a reliable source), because if not, you could be opening yourself up to cross-site scripting attacks. The reason your approach didn’t work is that Razor HTML-encodes output by default (every time you use @ … Read more