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

How to validate one field related to another’s value in ASP .NET MVC 3

One possibility is to write a custom validation attribute: public class RequiredIfOtherFieldIsNullAttribute : ValidationAttribute, IClientValidatable { private readonly string _otherProperty; public RequiredIfOtherFieldIsNullAttribute(string otherProperty) { _otherProperty = otherProperty; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { var property = validationContext.ObjectType.GetProperty(_otherProperty); if (property == null) { return new ValidationResult(string.Format( CultureInfo.CurrentCulture, “Unknown property {0}”, new[] { _otherProperty … Read more

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

Using custom VirtualPathProvider to load embedded resource Partial Views

Because now you are serving your views from some unknown location there is no longer the ~/Views/web.config file which applies and indicates the base class for your razor views (<pages pageBaseType=”System.Web.Mvc.WebViewPage”>). So you could add an @inherits directive at the top of each embedded view to indicate the base class. @inherits System.Web.Mvc.WebViewPage @model …

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=, 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

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