开发者

Best practice for looping through Db data and displaying in Asp.net 4.0 without Webforms!

开发者 https://www.devze.com 2023-03-22 03:30 出处:网络
Assuming that the data has been retrieved from the Db and put into an object people. foreach (People p in people)

Assuming that the data has been retrieved from the Db and put into an object people.

foreach (People p in people)
{
    // create an html button for each p

     PlaceHolder1.Controls.Add("control");
}

Although it does not follow the MVC pattern, I would like a method that loop through the data in the controller. I have been trying to do this with dynamic controls which has turned into a major nightmare.

EDIT: I would like to use Webform but not for the creation of dynamic content. If for example you created a CRUD system with dynamic controls against a db, the complexity of keeping the creation, deletion in the db in sync with the actions in the page lifecycle is fairly great. For all other static content, webforms are actually pretty decent.

So, to recap, the solution I am looking for would actually exist in a webforms page, however, the loop would not create dynamic webform controls, but rather standard html开发者_如何学运维 controls.


You would pass the collection to the view. Then iterate through each item in the collection and add the html code there.


If this is a MVC site (and since you mentioned not WebForms I assume it is), you REALLY want to do it in your view. It's both easy and proper to do it there. Doing it in the controller means that to output it you're going to have to override the result and return your results rather then what's in the view, which is not the way to go.

If you need to perform some kind of action on the data, then looping through it in the controller makes sense (and you already have the code to do that). But not if your goal is to create buttons. Do that in the view.

Can you clarify what you're trying to accomplish? Maybe that will help some, but all I can recommend right now is to not do what you're doing and instead create the buttons with a loop in the view.


If you have something really special that for some reason you can't use Views for(you said not webforms and not MVC?), then I have just the project for you. I created a little project named EView for very simple rendering of HTML(or anything else) as a lightweight compile-time view. I'll admit the documentation is a bit out of date, but most of what's out of date is just features that were added. It's BSD licensed.

An example:

Templates/SomeView.html:

{@ Title as string; @}
<html><head>
<title>{=Title=}</title>
<body>
Hey check out the dynamic title
</body>
</html>

SomeCode.cs:

var view=new SomeView();
view.Title="Some View Title";
response.Write(view.EViewRender()); //render returns a string


If you'd prefer to not have either a MVC View nor traditional ASP.NET WebForm, you could create a http handler to spit out your results directly to the browser

 <!-- Register you're handler web.config -->
 <system.web>
 ...
     <httpHandlers>
         <add verb="*" path="People.axd" type="YourLibNameSpace, PeopleHandlerFactory" />
     </httpHandlers>
 ...
 </system.web>

 public class PeopleHandlerFactory: IHttpHandlerFactory
 {
      public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {

         Page p = new Page();
        p.SetRenderMethodDelegate(delegate (HtmlTextWriter writer) 
            {
                // ... some standard html tags if so desired
                foreach (var person in People)
                {
                    writer.Write(person.ToString());
                }
            });
         return p;
    }
 }

But if you're using MVC views, your controller would be setup like this

public class PeopleController : Controller
{
    public ActionResult Index()
    {
        var people = Repository.GetPeople();
        return View(people);
    }
 ....
 }

Your View (Razor .cshtml) would look something like the following

www.site.com/people/

@model IEnumerable<People>
@foreach (var person in Model)
{
    @person.Name
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号