自定义模板asp.mvc问题 [英] custom template asp.mvc problem

查看:106
本文介绍了自定义模板asp.mvc问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我强类型的视图,对于字符串类型的属性自定义模板,我应该改变值

  //模型
类Person
{
  公共字符串名称{;组; }
}//视图
@model人
< D​​IV>
    @ Html.EditorFor(M = GT; m.Name)
< / DIV//自定义模板视图
@model System.String
@ Html.TextBox(的String.Empty,Model.ToUpper())

但它不工作 - 我得到老字号的价值,并未改变。

我忘了重要的细节 - Name属性得到的URL查询字符串值 -

 的http://本地主机:53494 /名称=胡说


解决方案

HTML辅助,如文本框总是首先考虑的ModelState结合自己的价值时,因为已经有一个值与请求字符串来它忽略了你传递给它的第二个参数。因此,要实现你在找什么,您可能需要首先从模型状态删除值:

  @model串
@ {
    ViewData.ModelState.Remove(ViewData.TemplateInfo.GetFullHtmlFieldName());
}
@ Html.TextBox(的String.Empty,(型号??的String.Empty).ToUpper())

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

解决方案

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

这篇关于自定义模板asp.mvc问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆