Html.Encode 将 Id 转换为选定的文本
所以我的观点是这样的:
<%= Html.Encode(Model.STPData.ProjectName)%>
现在说我没有将 ProjectName
作为字符串,而是只有 ProjectId
。我如何让它显示 Id 对应的文本字符串?包含所有文本/值对的下拉列表 - 我能够生成,但它不存储在任何地方。
我很困惑。谢谢。
So I have this on my view:
<%= Html.Encode(Model.STPData.ProjectName)%>
Now say instead of having the ProjectName
as the string, I only had the ProjectId
. How would I make this display the string of the text that the Id corresponds to? The dropdown list that contains all the text/value pairs - I am able to generate but it isn't stored anywhere.
I'm stumped. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于名称的存储位置。
最好是确保模型具有名称,任何其他解决方案都是打破 MVC 模式,因为视图永远不应该访问模型不包含的任何信息。
但是,如果您无法修改模型,请创建一个使用 id 获取名称的方法,并将其包装在 id 变量周围。
That would depend on where the name is stored.
Best would be to make sure the model has the name, any other solution would be to break the MVC pattern as the view should NEVER access any information that the model does not contain.
But if you cannot modify the model, make a method that fetches the name using the id and wrap that around the id variable.
如果视图需要显示信息,您可以修改视图模型以包含此信息,然后简单地:
控制器有责任填充此视图模型属性,以便视图不应该想知道从哪里获取此信息,因为这不是它的责任。
If a view needs an information to display you modify your view model to include this information and then simply:
where the controller has the responsibility to populate this view model property so that the view shouldn't wonder where to fetch this information from because that's not its responsibility.