编辑器+模型
这个非常有用的方法的签名表明我可以指示一个类型:
public static MvcHtmlString EditorFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
...文档非常容易理解地指出 TModel 是“模型的类型”。这一定是我的特殊倾向,这个描述对我来说没有任何意义。我用谷歌搜索了解释,但没有找到。
我处于 @model Website.Models.Product
的视图中,但想要为不同类型的内容创建编辑器。我想我可以:
@Html.EditorFor(@ViewBag.AClassOfTheOtherType)
或者也许(我显然是在猜测):
@Html.EditorFor(TheOtherType)
但这不是可接受的语法,所以我想:
@Html.EditorFor(x => x...)
但是 lambda 表达式似乎绑定到 @model
...所以我想,“啊!”:
@Html.EditoFor<TheOtherType>(...)
但是VS认为<启动一个 HTML 标记并指示 EditorFor 调用的结束(失败)。
啊啊啊啊!
我该怎么做(如果我真的需要问)?
the signature for this very useful method states I can indicate a type:
public static MvcHtmlString EditorFor<TModel, TValue>(
this HtmlHelper<TModel> html,
Expression<Func<TModel, TValue>> expression
)
... the docs very understandably state that TModel is "The type of model". It must be my particular bent that this description conveys no meaning to me whatsoever. I've googled for an explanation but found zilch.
I'm in a view where @model Website.Models.Product
but want to create an editor for something of a different type. I thought I could:
@Html.EditorFor(@ViewBag.AClassOfTheOtherType)
or maybe (I'm obviously guessing):
@Html.EditorFor(TheOtherType)
but that is not acceptable syntax and so I thought:
@Html.EditorFor(x => x...)
but the lambda expression seems to be bound to @model
... so I thought, "ah!":
@Html.EditoFor<TheOtherType>(...)
but VS thinks the < starts an HTML tag and indicates the end of my EditorFor call (which fails).
aaaaahhhhh!
how do I do this (in case I actually need to ask)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
还没有测试过这一点,但你不能这样做:
你不一定需要使用传递到 Lambda 的变量。
Haven't tested this, but couldn't you do:
You don't necessarily need to use the variable passed into the Lambda.
Model - 是模型 - MVC
TModel 中的 M 也可以是 sdserweJJG - 它只是不符合惯例地一致称为 TModel。
在 EditorFor 的辅助方法中,您将看到类似的内容:
这是扩展方法编译传入的 lambda 的地方 - 例如 x=>x.Model.Property 部分 - 并返回实际的值用于构建实际显示控件的模型数据。
当您从控制器操作调用
return View(viewModel);
时,模型将传递到视图。您尝试做的事情没有意义,因为该方法被设计为与视图模型一起使用。
但是,您可以使用 @Html.Editor,因为这将以您尝试的方式获取实际值:
MVC 的源代码可以免费下载和查看 - 非常值得花时间这样做:)
Model - is the Model - the M in MVC
TModel could just as well be sdserweJJG - it's only consistently called TModel out of convention.
Within the helper method for EditorFor you will see something like:
this is where the extension method compiles the lambda passed in - for example the
x=>x.Model.Property
part - and gets back the actual Model data to use to build the actual display controls.The Model is passed to the view when you call
return View(viewModel);
from your controller action.What you are trying to do doesn't make sense because the method was designed to work with the views Model.
You can however use
@Html.Editor
as this will take the actual value in the way you are trying:The sourcecode for MVC is freely available to download and view - it's well worth taking the time to do so :)
答案是(请打鼓)...是的,可以将 lambda 表达式与类型声明符绑定。唯一的问题是 Visual Studio 编辑器,它认为结束 C# 部分并以开头 << 进入 HTML 部分。因此不允许正确的代码。解决方案:
the answer is (drumroll please)... yes, one can bind the lambda expression with the type declarator. the only problem is the Visual Studio editor, which thinks one is ending the C# part and entering the HTML part with the opening < and thus disallows proper code. Solution: