genxgeek
genxgeek

Reputation: 13347

string.Parse equivalent for ModelBindingContext?

What is the string equivalent of the following modelbindingcontext int.parse code below?

int myint = int.Parse(valueProvider.GetValue("MyId").AttemptedValue);

Would like to use string.Parse ... but not defined

string mystring = string.Parse(valueProvider.GetValue("MyName").AttemptedValue);

Upvotes: 0

Views: 119

Answers (1)

bhamlin
bhamlin

Reputation: 5187

The quick and easy answer is that AttemptedValue is already a string. So I'm not sure what the point of parsing it or calling ToString() on it is.

The more correct way of doing both this and your int scenario is to use the ConvertTo() method of the ValueProviderResult.

int myInt = valueProvider.GetValue("MyId").ConvertTo(typeof(int));
string myString = valueProvider.GetValue("MyName").ConvertTo(typeof(string));

Upvotes: 1

Related Questions