what is the equivalent of the “literal” tag in asp.net mvc

i am migrating a website from asp.net to asp.net mvc

in the asp.net site there are many places where they have “literal” tags and the server generates a bunch of html and sticks in in the literal tag.

what is the equivalent of doign this in asp.net mvc. Should i shove this in ViewData?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The equivalent is to not make the same bad design choices the original developer made.

But, since we live in the real world, you’d shove their strings-which-contain-html into the presentation model for a particular page and then write it to the response stream.

In your Model:

public class MyPageModel
{
  public string HolyCrapItsHtml {get;set;}
}

In your controller:

public ActionResult MyPage()
{
  return View(new MyPageModel
         {HolyCrapItsHtml = OldCode.GetHtmlICantBelieveIt()});
}

And in your page:

<div>
  In the olden days, we'd concatenate our webpages together from strings like:
  <%= Model.HolyCrapItsHtml %>
</div>

Method 2

I had the same problem. Don’t bother with a MyPageModel, just stick with ViewData but use equals instead of the colon.

<%= ViewData("SomeVarName") %>

Method 3

If you’re using Razor, you’d do the following if not using strongly-typed helpers

@Model.YourVarName

If you’re working in with strongly-typed view model, you can use the ValueFor() helper

@Html.ValueFor(m => m.YourVarName)

If you just have a few literals, you could put them in the ViewData, but if you’re following a true MVC model you should create a model for the view with corresponding properties. Considering you’re porting from an existing ASP.NET page, setting up a new model for each new view/page may carry a lot ov overhead, in which case stick it in the ViewData.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x