custom template asp.mvc problem

∥☆過路亽.° 提交于 2019-12-25 08:58:56

问题


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

回答1:


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())


来源:https://stackoverflow.com/questions/5566089/custom-template-asp-mvc-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!