用lambda exp连接剃须刀中的特殊字符 [英] Concatenating special character in razor with lambda exp
本文介绍了用lambda exp连接剃须刀中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一个特殊字符:在我的剃刀视图中,目前通过lambda表达式绑定
@ Html.DisplayFor(x => x.StoreProfileAssociation.RegionalManager.Title)
我尝试了什么:
我试过这种方式但抛出异常
@ Html.DisplayFor(string.Format({0} {1},x => x.StoreProfileAssociation.RegionalManager.Title,:)
解决方案
在视图
下创建名为 DisplayTemplates 的文件夹 - >共享
- >DisplayTemplates
文件夹
< br $>
- 观看次数
- 分享
- DisplayTemplates
在DisplayTemplates文件夹中创建一个视图MyFormat.cshtml
写下面的代码在自定义格式的cshtml文件中ing $ / $
@ model string
@ string.Format( {0}:,型号)
然后你的剃须刀为
@ Html.DisplayFor(model = > model.PropertyName, MyFormat)
你的问题是:你对string.Format
的调用的第一个参数是委托实例,这根本没有意义。你需要一些价值,而不是寻找价值的方法;然后格式化的字符串将包含在该对象上调用的System.Object.ToString()
返回的任何内容。
-SA
Hi ,
I wanted to add an special character : in my razor view which is currently binded via lambda expression
@Html.DisplayFor(x => x.StoreProfileAssociation.RegionalManager.Title)
What I have tried:
I tried in this way but throws an exception
@Html.DisplayFor(string.Format("{0}{1}", x => x.StoreProfileAssociation.RegionalManager.Title,":")
解决方案
Create a Folder named DisplayTemplates underViews
->Shared
->DisplayTemplates
Folder
- Views
- Shared
- DisplayTemplates
Create a view asMyFormat.cshtml
inside DisplayTemplates Folder
write the following code in the cshtml file for custom formatting
@model string @string.Format("{0}:",Model)
and then your razor as
@Html.DisplayFor(model => model.PropertyName,"MyFormat" )
referred from Display For Templates[^]
Your problem is: first parameter of your call tostring.Format
is the delegate instance, which makes no sense at all. You need some value instead, not the method to find value; and then the formatted string will include anything returned bySystem.Object.ToString()
called on that object.
—SA
这篇关于用lambda exp连接剃须刀中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文