103 articles ASP.NET MVC Page 10 / 11

ASP MVC Customize Error Message Language

Tambahkan baris berikut di fungsi Application_Start() pada Global.asax [code language=”csharp”] ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyGlobalErrors"; DefaultModelBinder.ResourceClassKey = "MyGlobalErrors"; [/code] Buat MyGlobalErrors.resx di dalam folder App_GlobalResources, tambahkan nilai berikut: FieldMustBeDate The field {0} must be a date. FieldMustBeNumeric The field {0} must be a number. PropertyValueInvalid The value ‘{0}’ is not valid for {1}. PropertyValueRequired A value is…

ASP Bootstrap Tooltip Based on Model Annotation

Model: [code language=”csharp”] [Required] [DisplayName("Nama")] public string Name { get; set; } [/code] Javascript: [code language=”javascript”] $("input[type=text]").tooltip({ trigger: "focus", title: function () { var title = ""; var titleArray = []; if ($(this).attr("data-val-required") != null) titleArray[titleArray.length] = $(this).attr("data-val-required"); if ($(this).attr("data-val-number") != null) titleArray[titleArray.length] = $(this).attr("data-val-number"); title = titleArray.join(" "); return title; }, }); [/code]

ASP.NET Required Asterisk using jQuery

[code language=”javascript”] $(‘input[type=text], textarea, select’).each(function () { var req = $(this).attr(‘data-val-required’); if (undefined != req) { var label = $(‘label[for="’ + $(this).attr(‘id’) + ‘"]’); var text = label.text(); if (text.length > 0) { label.append(‘<span style="color:red"> *</span>’); } } }); [/code]

MVC 4 @Scripts or @Styles “does not exist”

1. Install Microsoft.AspNet.Web.Optimization using NuGet 2. Add “<add namespace=”System.Web.Optimization”/>” to both web.configs (root and view) 3. Call the RegisterBundles() function from Application_Start() in your global.asax.cs: [code language=”csharp”] using System.Web.Optimization; protected void Application_Start() { … BundleConfig.RegisterBundles(BundleTable.Bundles); … } [/code] http://stackoverflow.com/questions/10951669/mvc-4-scripts-does-not-exist http://stackoverflow.com/questions/9475893/how-to-add-reference-to-system-web-optimization-for-mvc-3-converted-to-4-app

ASP MVC Unit Test ModelState Always Valid

[code language=”csharp”] ProductModelEdit model = new ProductModelEdit() ; //Init ModelState var modelBinder = new ModelBindingContext() { ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType( () => model, model.GetType()), ValueProvider=new NameValueCollectionValueProvider( new NameValueCollection(), CultureInfo.InvariantCulture) }; var binder=new DefaultModelBinder().BindModel( new ControllerContext(),modelBinder ); ProductController.ModelState.Clear(); ProductController.ModelState.Merge(modelBinder.ModelState); ViewResult result = (ViewResult)ProductController.CreateProduct(null,model); Assert.IsTrue(result.ViewData.ModelState["Name"].Errors.Count > 0); Assert.True(result.ViewData.ModelState["Description"].Errors.Count > 0); Assert.True(!result.ViewData.ModelState.IsValid); [/code] http://stackoverflow.com/questions/286124/how-can-i-test-modelstate

ASP MVC Unit Test Not Detected

Please add the keyword public to your class definition. Your test class is currently not visible outside its own assembly. [code language=”csharp”] namespace tests { [TestClass] public class SimpleTest { [TestMethod] public void Test() { Assert.AreEqual("a","a", "same"); } } } [/code] http://stackoverflow.com/questions/13533259/why-does-visual-studio-2012-not-find-my-tests

CKEditor with ASP.NET MVC Bundling

Using CKEditor in ASP.NET MVC 4 still has problems. The system will load required files when using ckeditor, but because of the name ckeditor.js disappeared, the path of required files become false. For that reason, in bundling, CKEditor must have its own bundle with name like “~/Scripts/ckeditor” if place in Scripts directory, or like “~/Scripts/wcw/ckeditor…

ASP.NET MVC4: How-To Bundling if Using Subdirectory

In ASP.NET MVC4, if you want to have your css files or javascript files well categorized into directory-directory, there are some problems when you want to publish the application. If you’re using bundleConfig like this: [code language=”csharp” highlight=”3,9,13,14″] bundles.Add(new ScriptBundle("~/bundles/app").Include( "~/Scripts/jquery-ui.custom.js", "~/Scripts/bootstrap/bootstrap.js", "~/Scripts/jquery.flot.js", "~/Scripts/jquery.flot.resize.js", "~/Scripts/jquery.peity.js", "~/Scripts/jquery.uniform.js", "~/Scripts/jquery.jpanelmenu.js", "~/Scripts/wcw/ckeditor.js" )); bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap/bootstrap.css", "~/Content/bootstrap/bootstrap-responsive.css", "~/Content/jquery-ui.css", "~/Content/jquery.gritter.css"…

IIS: Virtual Directory Not Using web.config from Parent Directory

As we know, when application is placed on virtual directory, the web.config (or configuration) also will be inherited the configuration of parent directory application. Sometimes it will cause problems because parent directory contains library (.dll) that doesn’t exist in virtual directory application. So, for the virtual directory application only using web.config itself, the web.config of…