c# - Returns only a single row for multiple same id in asp.net mvc -


i'm using asp.net mvc 4 , ef 6 make message panel want show list of single threads. if thread viewed, user can see messages under specific thread. in db, each message has same thread id that, messages can viewed under specific thread. far user can view messages under specific thread thread lists showing each message thread. here codes,

controller

    public actionresult viewallthreads(string msgpanelid)     {         var getuser = rentdb.ownerregs.where(a => a.username == user.identity.name).firstordefault();          inboxmodel allthreads = new inboxmodel() { allmsg = db.msgtodb.where(a => a.owner_id == getuser.serial).tolist() };         viewbag.ownerid = getid;          return view(allthreads);     } 

view

@foreach (var item in model.allmsg) {         <tr>             <td class="text-center">@html.displayfor(modelitem => item.name)</td>             <td class="text-center">@html.displayfor(modelitem => item.title)</td>             <td class="text-center">@html.displayfor(modelitem => item.addedtime)</td>             <td class="text-center">@html.encodedaction("<b>view thread</b>", "reply", "home", new { thread_id = item.thread_id, msg_id = item.serial })</td>         </tr> } 

how can make list messages same thread id show single thread? need badly! thanks.

updates

here snap of table design,

table_design

but thread lists showing each message thread

because querying. see line of code.

 db.msgtodb.where(a => a.owner_id == getuser.serial).tolist()  

you querying messages. if want threads, should querying threads table/entities.

so add new property view model list of threads.

public class threadviewmodel {   public int id {set;get;}   public string title {set;get;} } public class inboxviewmodel {    public list<threadviewmodel> threads {set;get;}    public inboxviewmodel()    {      threads= new list<threadviewmodel>();    } } 

and in action method,

public actionresult viewallthreads(string msgpanelid) {     var getuser = rentdb.ownerregs.where(a => a.username == user.identity.name)                                   .firstordefault();     var vm=new inboxviewmodel();     vm.threads = rentdb.threads.where(a => a.owner_id == getuser.serial)                 .select(t=>new threadviewmodel { id=t.id,title=t.title}).tolist();       return view(vm);  } 

assuming rentdb.threads returns collection of thread entities.

now viewallthreads.cshtml view should typed our view model

@model inboxviewmodel <table>  <td><th>title</th> @foreach (var item in model.allmsg) {         <tr>                       <td class="text-center">@html.displayfor(modelitem => item.title)</td>                    <td class="text-center">@html.actionlink("view thread", "reply", "home",                                   new { threadid = item.id },null)</td>         </tr> } </table> 

now show messages under thread, may query messages under thread in reply action method

public actionresult reply(int threadid) {   var threads = rentdb.messages.where(s=>s.thread_id==theadid).tolist();   var vm =new inboxmodel() { allmsg = threads };   return view(vm); } 

you may add more null/security checking method needed.


Comments