views:

71

answers:

2

Hi guys

My english very terrible, so i cant explain very clear. I will try my best.

is it possible to generate HTML code from within ASP.NET MVC, please?

This is Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Hello ASP.NET MVC!";
        ViewData["author"] = "Author: Alex";


        return View();
    }

This is Views, It's a template.

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
title

<%: ViewData["Message"] %>

<%=ViewData["author"]%>

<%=Html.ActionLink("This is a link.", "Index", "About") %>

I want to use this template generate html file.


Have a nice day,

Alex

+2  A: 

Yes.

You can generate HTML by adding C# code to a view.

SLaks
+3  A: 

By generate html I'm assuming you mean programmatically and are not referring to views which of course you can put html in.

In a html helper or controller you can use the TagBuilder to generate html. For example..

TagBuilder tag = new TagBuilder("img");
tag.Attributes.Add("id", "myImage");
tag.Attributes.Add("src", "/Content/Images/some_image.png");
tag.Attributes.Add("alt", "my image");
tag.Attributes.Add("width", "300");
tag.Attributes.Add("height", "300");

string html = tag.ToString();

You can also use HtmlTextWriter which is a little bit similar.

HtmlTextWriter writer = new HtmlTextWriter(stream);
writer.RenderBeginTag(HtmlTextWriterTag.Ul)
// dome some stuff
writer.RenderEndTag();

etc.

Joshua Hayes