开发者

Html.Encode translating the Id into it's selected Text

开发者 https://www.devze.com 2023-02-13 06:51 出处:网络
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 st

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.开发者_运维技巧


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:

<%= Html.Encode(Model.ProjectName)%>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消