Reputation: 912
I'm developing a new application in ASP.Net MVC (beginner) from old application which is in ASP.Net and have one query that How should I convert below line of code into new in MVC?
HTML:
<div runat="server" id="dvLogList"></div>
.cs:
dvLogList.InnerHtml = sb.ToString()
I need to set the StringBuilder
appended string
html
code to as dvLogList
innerhtml
Upvotes: 2
Views: 7588
Reputation: 2318
Use below code :
//Controller Action
public ActionResult Index()
{
ViewBag.HTMLContent = "Your HTML Data";
return View();
}
//View page code
<div id="dvLogList">
@Html.Raw((String)ViewBag.HTMLContent)
</div>
Upvotes: 0
Reputation: 2978
You can strongly-typed your view.
As an example I have a model:
class DisplayModel
{
public string str { get; set; }
}
In my controller I will pass this model to my view:
public ActionResult Display()
{
DisplayModel model = new DisplayModel
{
str = sb.ToString()
}
return View(model);
}
The next step is to make my view strongly typed. To make this possible add this line to the top
@model DisplayModel // Most of the cases you need to include the namespace to locate the class
<div id="dvLogList">@Model.str</div> // now your model is accessible in view
In the end, why are we doing this? This one has the advantage compared to using viewbag because there are cases that we need to postback data to our controller. The values from your view were automatically binded to your model (given that you declare the model in your action).
// model is automatically populated
public ActionResult Save(DisplayModel model)
{
}
For further knowledge, read this link I cannot spare more time to improve this answer Strongly Typed Views
Upvotes: 3
Reputation: 1222
You can do this by following way:
in your controller action that invokes this view:
public ActionResult Index()
{
ViewBag.HTMLContent = "your HTML Content";
return View();
}
In Index.cshtml view:
<div id="dvLogList">
@Html.Raw("@ViewBag.HTMLContent") /*assuming your content is HTML content not just string*/
</div>
Upvotes: 0
Reputation: 394
In your controller, make use of ViewData (or ViewBag)
ViewData["dvLogList"] = "whatever content you want";
In your view you can call the ViewData wherever you need it:
<div id = "dvLogList" >
@(new HtmlString(ViewData["dvLogList"].ToString()))
</div>
Hope this helps.
Upvotes: 2