1

i have some issue for view in cshtml page.. i already define foreach like this

<table>
<tr>
<th>Type</th>
<th>Name</th>
</tr>
@foreach (var item in Model)
 {
<tr>
       <td>  @Html.DisplayFor(modelItem => item.typeBook)</td>
       <td>  @Html.DisplayFor(modelItem => item.bookName)</td>
<tr>    
 } 
</table>

and the view

     Type | Name
   Horror | Scary
   Horror | Spooky
    Jokes | Bean

but i want the view like this..

  Type | Name
Horror | Scary , Spooky
 Jokes | Bean

this my view model

public class BookViewModel 
    {


        [DataMember]
        public int IdBook { get; set; }

        [DataMember]
        [DisplayName("type Book")]
        public string typeBook { get; set; }

        [DataMember]
        [DisplayName("book Name")]
        [Required]
        public string bookName { get; set; }


    }

for select to database i use linq Exp

Expression<Func<Book, bool>> criteria = c => c.IdBook == (int)IdBook ; 
IOrderedEnumerable<BookViewModel> result = this.GetList(criteria).OrderBy(o => o.typeBook);
return result.ToList<BookViewModel>();

can anyone have idea for this?? any reference,sugestion will help.. Thanks !

Anonim
  • 61
  • 1
  • 13

2 Answers2

1

Instead of customizing or writing any logic in razor view, You should pass custom ViewModel to your view. And you can use GroupBy query to create custom ViewModel field from your list. View should contain only render syntax.

Inside your controller update your method with following code :

// books is your original model data
var bookListViewModel = books.GroupBy(b => b.typeBook)
                                .Select(b => new BookViewModel()
                                {
                                    type = b.Key, 
                                    name = string.Join(",", b.Select(book => book.bookName))
                                });

So now you would have in BookViewModel, typeBook = "Horror" and bookName = "Scary , Spooky" as you need to display.

So in your view you can use it same as you are using. Just you have to pass ViewModel to your view now.

Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
  • 1
    That's exactely what I proposed in the comments above. – Complexity Jun 05 '14 at 07:16
  • maybe can use groupby if just 2 value i mean typeBook,nameBook but if 3 value like typeBook,Writer,NameBook ,thats can't use group by. – Anonim Jun 05 '14 at 07:24
  • @user3645707 I am not getting you, can you please show me how you want to display data in view. – Mukesh Modhvadiya Jun 05 '14 at 07:37
  • i already use group by in Ling Expresion but in view see what i want on my question,, doesn't matter for type but i want book not repeat just like @Html.DisplayFor(modelItem => item.bookName), seperate with comma – Anonim Jun 05 '14 at 07:38
  • @user3645707 Please look at the updated answer, I have written code how to create view model with GroupBy query. Hope it helps. Mark answer as accepted if it helps. – Mukesh Modhvadiya Jun 05 '14 at 09:32
  • I personally would have no issue with doing the `GroupBy` in the `Razor` view, since it is view logic, and presumably, it is legal to have a second view that displays the items denormalized. – Aron Jun 05 '14 at 09:47
  • i use linq for select to database .. Expression> criteria = c => c.IdBook == (int)IdBook ; IOrderedEnumerable result = this.GetList(criteria).OrderBy(o => o.typeBook); return result.ToList(); can you help me with this. – Anonim Jun 05 '14 at 11:14
  • @user3645707 do not select ViewModel directly from context or repository, select Book List from context, and convert it to ViewModel in business or controller class as I have shown in answer where "book" is list of your Book entity. – Mukesh Modhvadiya Jun 06 '14 at 05:30
0

You have to implement your query as group by Type when you assign model in controller

e.g.

    var query = BookViewModel.
                Orderby(x =>x.bookName).
                GroupBy(x =>x.Type);

further query for three fields

 var query = BookViewModel.
                Orderby(x =>x.bookName).
                GroupBy(x =>x.Type,  n => n.Writer);
MSTdev
  • 4,507
  • 2
  • 23
  • 40