user2067554
user2067554

Reputation:

How to add orderby in a controller

I know guys this question is not that hard but I cant seem to figure out how to add orderby to my controller. Sorry guys I'm noob :(. This is my scenario.

I have a list of tags on my view. Whenever I select a tag, a list of post will appear with the same tagname of the selected tag. My codes works fine. Only that whenever I select a tag, the list that it shows is in descending order. The latest one is on the bottom. I want it on top. :(

CURRENTLY MY OUTPUT
3
2
1

WHAT I WANT TO ACHIEVE
1
2
3

This is my controller. I can't seem to figure out how to do the logic of orderby in my browse.

    public ActionResult PostTags() // This is one is ok
    {
        IQueryable<Tag> tags = from tg in db.Tags
                               orderby tg.Name ascending
                               select tg;
        return View(tags);
    }

   public ActionResult Browse(string tag) // This is where I can't seem to figure out how add orderby 
    {
        var tagModel = db.Tags.Include("Posts").Single(ts => ts.Name == tag);

        return View(tagModel);
    }

Thanks in advance for anyone who could help me.

UPDATE: This is my Post tag view

@model IEnumerable<Blog.Models.Tag>

@{
    ViewBag.Title = "PostTags";
}

<h3>Tags</h3>

@foreach (var item in Model)
{ 
    <ul>
            <li>
            @Html.ActionLink(item.Name, "Browse", "Post",
                   new { Tag = item.Name }, null)
           </li>
    </ul>
}

//and this is my browse View
@model List<ProjectPAL.Models.Tag>

@foreach (var tag in Model.Posts)
{   

  <div><a href="@Href("~/Post/Details/" + tag.ID)">@tag.Title</a></div> 
   <a href="@Href("~/Post/Details/" + tag.ID + "#leavecomment")">Add a Comment</a>          
}

Upvotes: 3

Views: 744

Answers (3)

Carlos Corral Carvajal
Carlos Corral Carvajal

Reputation: 526

When you return the tagModel to the view, I suppose that you render the Posts through a foreach loop, something like this:

@foreach(var post in Model.Posts)
{
    //your html
}

If I'm right, you can do this to order the post like you want:

@foreach(var postin Model.Posts.OrderByDescending(p => p.PostID))
{
    //your html
}

Upvotes: 0

Shyju
Shyju

Reputation: 218812

I am guessing you want to get the collection of Post's of the purticular Tag. Assuming you have your entities like this

public class Post
{
    public int ID { set; get; }
    public string Title { set; get; }
    public virtual ICollection<Tag> Tags { set; get; }
}
public class Tag
{
    public int ID { set; get; }
    public string Name { set; get; }
    public virtual ICollection<Post> Posts { set; get; }
}

and you have overridden the OnModelCreating method of your DBContext class to create a third table to store your PostId and TagId (Assuming it is a many-to-many relationship)

Then you can update your action method code like below to get the Posts which has a tag same as what user passed to the action method. You can add the OrderBy method to the result of the Where method.

public ActionResult Browser(string tag)
{
    using (MyDBContext db = new MyDBContext())
    {
        var postsForTag = db.Posts
                           .Where(x => x.Tags.Any(o => o.Name == tag)) 
                           .OrderBy(y => y.ID).ToList();

        // TO DO : Return the above collection / appropriate view model.         

    }
}

EDIT : In your earlier comment you said,you want to show a list of Post's for the tag, so you should update your view to have it strongly typed to a collection of Post's .

@model List<Blog.Models.Post>
<h2>Posts</h2>
@foreach(var item in Model)
{
  <p>@item.Title</p>
}

Upvotes: 4

Moby&#39;s Stunt Double
Moby&#39;s Stunt Double

Reputation: 2550

Depending on how your navigation properties are set up, I would search the posts, rather than the way you did it:

var tagModel = db.Posts.Where(p => p.Tag.Name == tag).OrderBy(p => p.PostPropertyYouWantToOrder);

What you were doing is making the statement retrieve all tags with posts, then narrowing it down. Go for the path of least resistance.

Upvotes: 2

Related Questions