0

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
4
  • what does it give you instead? Commented Apr 6, 2011 at 12:14
  • it give old Model.Name, value doesn't change Commented Apr 6, 2011 at 12:15
  • 1
    What are you trying to achieve? What do you mean under not working? Commented Apr 6, 2011 at 12:16
  • i try change value for Name field, but i get original value from query string in url Commented Apr 6, 2011 at 12:43

1 Answer 1

2

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())
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.