Boo
Boo

Reputation: 1654

custom template asp.mvc problem

i have strongly typed View, custom template for properties of string type, i should change value

// model
class Person
{
  public string Name { get; set; }
}

// view
@model Person
<div>
    @Html.EditorFor(m => m.Name)
</div

//custom template view
@model System.String
@Html.TextBox(string.Empty, Model.ToUpper())

but it doesn't work — i get old Name value, not changed

NEW

i forgot important detail — Name property get value from query string in URL —

http://localhost:53494/?Name=blah

Upvotes: 0

Views: 208

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039468

Html helpers such as TextBox always first look into modelstate when binding their value and because there is already a value coming from the request string it ignores the second argument you are passing to it. So to achieve what you are looking for you might need to first remove the value from model state:

@model string
@{
    ViewData.ModelState.Remove(ViewData.TemplateInfo.GetFullHtmlFieldName(""));
}
@Html.TextBox(string.Empty, (Model ?? string.Empty).ToUpper())

Upvotes: 2

Related Questions