0

Is it possible to do something like this in regular ASP.NET (or something similar):

<ul>
    <% foreach (var item in Model) { %>       
        <li id="<%: item.Id %>">    
            blah blah     
        </li>       
    <% } %>
</ul>    

I need to do a gird, but I want to control how the html table is output.

4
  • Yes You can do. google.com/… Commented Mar 24, 2011 at 19:36
  • you can use a Repeater which allows you to markup your own html. Commented Mar 24, 2011 at 19:38
  • code blocks aren't unique to MVC, you would use the standard <%...%> Commented Mar 24, 2011 at 19:39
  • 1
    I stared at this for a minute, trying to get my head around why it looked so odd. Then I suddenly realized it's exactly how one used to do things (out of necessity) all the time using old-school asp! Commented Mar 24, 2011 at 19:44

3 Answers 3

1

Yes, the code written this way behaves as if it was written in code behind. If you want to have both, that works too. Expose a property in the page code behind class that is the Model and then you can access it the way you have shown.

public partial class _Default : System.Web.UI.Page
{
    protected List<WhateverClass> Model { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Populate the model here or in another appropriate event
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, something like this is what I was looking to do.
0

It is possible since MVC just uses the ASP.NET rendering engine (where the <% server tags come from). However, you should probably use the repeater control as @Eric mentioned. The problem is that the Model may not be initialized at the time your page is compiled and you are likely to get a null reference exception.

Comments

0

Yes, you can do it, but when in web forms you should probably do things the "web forms way" (or else why use web forms).

In your example you can accomplish the same thing with a repeater.

Note that this stylistic advice; both approaches are 100% correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.