Results 1 to 12 of 12

Thread: [RESOLVED] Decrement displaying of data

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Resolved [RESOLVED] Decrement displaying of data

    I want to make a "announcements" page and what I want to happen is that when a user posts a news, the post will be saved into the database and is defined by a unique ID (using autonumber). So data is arranged by that.

    When a user loads the page, announcements will be displayed from lastest to oldest.

    So what I'm trying to ask here is how can I list data from latest to oldest.

    The container of the announcements is below
    Code:
    <table >
        <tr>
            <th>
               Date Posted
            </th>
            <th>
                Author
            </th>
            <th>
                Title
            </th>
            <th></th>
        </tr>
    
    @foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.NewsDate)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Author)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.NewsTitle)
            </td>
    
            <td>
                 @Html.ActionLink("Read", "Details", new { id=item.ID })
            </td>
        </tr>
    }

    Edit: Is there a way that " @Html.DisplayFor(modelItem => item.NewsTitle)" will funtion as a link similar to @Html.ActionLink("Read", "Details", new { id=item.ID })?
    Last edited by azsx123; May 20th, 2011 at 12:19 PM.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Decrement displaying of data

    Hello,

    Looks to me like you are doing ASP.Net MVC, so I am going to more this question over to the ASP.Net MVC forum.

    Gary

  3. #3
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Decrement displaying of data

    Order the data when you retrieve it from your repository. (How depends how you query the data. LINQ in most my work. orderby dateValue descending)

    As for the 2nd question
    @Html.ActionLink(item.NewsTitle, "Read", "Details", new { id=item.ID })

    (think the parameters is displaytext, action, controller, attributes)


  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decrement displaying of data

    Quote Originally Posted by Krokonoster View Post
    Order the data when you retrieve it from your repository. (How depends how you query the data. LINQ in most my work. orderby dateValue descending)
    I'm using Sql server ce.

    Quote Originally Posted by Krokonoster View Post
    As for the 2nd question
    @Html.ActionLink(item.NewsTitle, "Read", "Details", new { id=item.ID })

    (think the parameters is displaytext, action, controller, attributes)
    I see. Thanks!

  5. #5
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Decrement displaying of data

    Do you have a domain model hooked up to your database (the latter not being important here)?

    How you query the domain model then? LINQ?

    Or do you directly query the database in your controller (hope not).

    Either case you can use orderby.


  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decrement displaying of data

    I don't use queries. None as of I know of. I i'm using the login function that came with the MVC 3 layout

  7. #7
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Decrement displaying of data

    Then where does that code you posted come from? That ain't part of the default template.


  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decrement displaying of data

    Are you pertaining to the controller?

    Code:
            // GET: /News/
    
            public ViewResult Index()
            {
                return View(db.BatchNews.ToList());
            }
    I've been looking for the quries myself but am unable to do so.

  9. #9
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Decrement displaying of data

    Pseudo code, trust you can figure it out:

    Code:
    IList<NewsItem> items = (from item in db.BatchNews
    						 orderby item.NewsDate
    						 select item).ToList();
    return View(items);


  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2010
    Posts
    133

    Re: Decrement displaying of data

    I do. Thanks so much! =)

    Edit: Its not working.

    Code:
            public ViewResult Index()
            {
                IList<NewsModel> items = (from item in db.BatchNews
                                         orderby item.ID descending
                                         select item).ToList();
                return View(db.BatchNews.ToList());
            }
    Last edited by azsx123; May 21st, 2011 at 01:01 PM.

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] Decrement displaying of data

    Hello,

    When you say something is not working, can you please provide some more information?

    i.e. what exactly isn't working, and what steps have you taken to test it.

    Was there an exception? If so, what was it? Did it not compile? If not, what was the error?

    In order to best help you out, you need to help us out by providing as much information as possible.

    Gary

  12. #12
    Hyperactive Member Krokonoster's Avatar
    Join Date
    Jan 2010
    Location
    Cape Town
    Posts
    448

    Re: Decrement displaying of data

    Quote Originally Posted by azsx123 View Post
    I do. Thanks so much! =)
    Code:
            public ViewResult Index()
            {
                IList<NewsModel> items = (from item in db.BatchNews
                                         orderby item.ID descending
                                         select item).ToList();
                return View(db.BatchNews.ToList());
            }
    return View(items);

    Second on what Gary said. Think carefully what information others will need to help you and include that. (That does not mean posting 100's lines of irrelevant code).


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width