1

How can I retrieve a property value from modelstate in httppost action.

Below is the code, I used to retrive the hidden id field from modelstate. But is it not possible to have strongly typed version to get value. Like, if property name is modified, prompting a compile time error.

Could anyone please explain difference between "AttemptedValue" and "RawValue".


 ModelState state;      
 if (ModelState.TryGetValue("id", out state))
 {
     string value = state.Value.AttemptedValue.ToString();
 }
Sunny
  • 4,655
  • 5
  • 35
  • 69

2 Answers2

1

Attempted value is used by the framework and it contains concatenated list of values. In my case, since it is id field, I am going ahead with attempted value. Below link has more information on this.

http://forums.asp.net/t/1571473.aspx/1?MVC+2+Custom+ModelBinder+and+storing+the+attempted+value+for+the+view

Sunny
  • 4,655
  • 5
  • 35
  • 69
0

you can iterate the ModelStateDictionary object and through the keys(property name) on the dictionary get the value of the desired property or you can do something like ModelState["PropertyName"].Value

John x
  • 3,991
  • 8
  • 40
  • 67