4

I've started a default MVC4 project in Visual Studio,

Somewhere in my model is this piece of code

 public class LoginModel
 {
    [Required]
    [Display(Name ="Name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
  }

I want to change it to something like this because i want to try localization (which works within another class, but not here) (strings=resx file for localization)

public class LoginModel
 {
    [Required]
    [Display(Name =strings.UserName)]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = strings.Password)]
    public string Password { get; set; }

    [Display(Name = strings.RememberMe)]
    public bool RememberMe { get; set; }
  }

The error is it must be a constant expression, but when I make it like that, then I get something like 'property index lacks accessor'

What am I missing here?? Why cant I just assign a string value to the darn thing? In Java this is all so much easier. Hope you can help me out.

rikitikitik
  • 2,414
  • 2
  • 26
  • 37
Bart Hofma
  • 644
  • 5
  • 19
  • 1
    possible duplicate of [Localization of DisplayNameAttribute](http://stackoverflow.com/questions/356464/localization-of-displaynameattribute) – Raphaël Althaus Jun 05 '12 at 08:10
  • 1
    thanks!! that post solved my problems, i had searched, but i guess i didnt search right. I had to use [Display(ResourceType = typeof(strings), Name = "Gebruikersnaam")] and i made the strings.resx attributes all public – Bart Hofma Jun 05 '12 at 08:33

2 Answers2

15

You should use Resources. In attributes you can set only immutable values.

Try this.

[Display(Name = "Remember me?", ResourceType = typeof(YourResourcesType))]
public bool RememberMe { get; set; }

And look this ASP.NET MVC 3 localization with DisplayAttribute and custom resource provider

Community
  • 1
  • 1
Alex Shkor
  • 1,209
  • 1
  • 10
  • 18
  • i already had the solution (see comment under question), but you are right, this is how to solve this problem. – Bart Hofma Jun 05 '12 at 11:04
-8

I think you must use sessions and viewState,

To use sessions Visit http://msdn.microsoft.com/en-us/library/ms178581.aspx

And to use viewstate Visit http://msdn.microsoft.com/en-us/library/ms972976.aspx