Rahul Hendawe Rahul Hendawe - 8 months ago 38
ASP.NET (C#) Question

How to use div by its Id in MVC Controller?

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?


<div runat="server" id="dvLogList"></div>


dvLogList.InnerHtml = sb.ToString()

I need to set the
code to as

Answer Source

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

<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