How to get all of default (English) validation messages, so I could translate them. I know I can specify resource name in validation attribute, but I just want to know all the messages.
To localize the default error messages in ASP.NET MVC you need to set the following properties in Global.asax Application_Start method,
ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyResources";
DefaultModelBinder.ResourceClassKey = "MyResources";
Next, you need to create a MyResources.resx resource file and your culture specific resource file MyResources.xx.resx inside App_GlobalResources folder where you can override the following messages:
PropertyValueInvalid
FieldMustBeDate
FieldMustBeNumeric
PropertyValueRequired
Related
I'm trying to set message for attribute this way in view model
[Required(ErrorMessageResourceName = nameof(Form.Required), ErrorMessageResourceType = typeof(Form))]
So i have my Form.resx file with Required key. I use PublicResXFileCodeGenerator tool and embedded resource type for resource file. I also have other files named form.culture.resx.
I'm also use this to set culture (current culture is "en" too now)
Thread.CurrentThread.CurrentCulture = new CultureInfo("en");
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(currentCulture);
at custom filter attribute.
The problem is that when I try to sumbit my form with empty field, i'm receiving the same message (in Russian).
i have a PropertyController, which I use to serve a bunch of pages. For example..
/Property
/Property/{id}
/Property/add
/property/edit/{id}
I now need to do a bunch of stuff based on a particular property I will need to do serve pages like this:
/Property/{id}/images/add
/Property/{id}/images/edit/{id}
/Property/{id}/rooms/add
/Property/{id}/rooms/edit/{id}
I think I need to build a new ImagesController and RoomsController, but do I need to but these in a folder structure? My RouteConfig is currently set to the default MapRoute rule ({controller}/{action}/{id}
You don't need to reflect your routing structure in your folders structure.
Check this one out:
ASP.Net MVC support for Nested Resources?.
Effectively your routing string is a regExpression to match whatever comes in from a requester. And if there's a match it's trying to bind all the variables in your expression to values from the HTTP request.
In regard to creating new controllers - a rule of thumb is to create a controller per resource / business entity. So in your case I would say yes to ImagesController, RoomsController and PropertyController.
Is there any possibility that we can give customized names for validation XMLs in Struts2.
Generally If our action class name is TestAction then validation XML name should be TestAction-actionName-validation.xml or TestAction-validation.xml.
But I want give validation in an XML like Abc.xml. Is it possible?
I have started working on an MVC project and I came across some scenarios where I feel I am stuck. I need to convert the existing MVC3 site to work for multiple language.
I have one HeaderPage.cshtml and it has a view model bound to it by
#model IHeaderPage
And it outputs a property of this model:
<h3>#Model.HeaderName</h3>
I called this view from MainPage.cshtml
#Html.Partial("HeaderPage")
Now in the Controller's Action method I change the model's property
objHeaderPage.HeaderName="Fill your Registeration details";
And when i run the project i see the the text "Fill your Registeration details".
Now how can I change the text value, i.e. it should read from my resx file.
I have already created resx files in App_LocalResources folder.
I heard that, it can be done by Display Attribute.. but how do i do that or is there any other better way?
This should answer your question regarding the use of DisplayAttribute.
I use DisplayAttribute for every property of my ViewModel, but if you have to handle custom messages like "The record can not be saved because of an error...", or something similar, you can simply use
objHeaderPage.HeaderName = Resources.ResourceMessageName;
The framework will choose automatically the correct culture.
I prefer to put all my resources in a separate projects so I can deploy only the dll of the resources in case of need, but you can also think to deploy the resx files to edit them directly on the production machine. I guess it's up to what you prefer/need.
use System.ComponentModel.DataAnnotations Namespace in ViewModel.
[Display(Name="Fill your Registeration details")]
public string HeaderName{get;set;}
also you can use your resource file. Just review http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx
I want to localize my resources (form labels, validation messages etc) using DisplayAttribute. I have my resources in the database, but looks like DisplayAttribute uses the resx files, and the class is sealed itself so I cannot derive a subclass and override the require methods/properties.
Is there any way of handling the resourcemanager used by DisplayAttribute, to get the resources from the database instead of the resx files.
And no, I can not use the resx files.
You will need to create your own ResourceProvider to get the messages from the database, and then set the <globalization> tag in Web.Config to point to your custom ResourceProvider. .Net will then use this instead of resx files.
Detailed explanation: http://msdn.microsoft.com/en-us/library/aa905797.aspx