Where should my "filtering" logic reside with Linq-2-SQL and ASP.NET-MVC in View or Controller?

Posted by Nate Bross on Stack Overflow See other posts from Stack Overflow or by Nate Bross
Published on 2010-03-19T17:52:33Z Indexed on 2010/03/19 18:11 UTC
Read the original article Hit count: 217

I have a main Table, with several "child" tables. TableA and TableAChild1 and TableAChild2.

I have a view which shows the information in TableA, and then has two columns of all items in TableAChild1 and TableAChild2 respectivly, they are rendered with Partial views.

Both child tables have a bit field for VisibleToAll, and depending on user role, I'd like to either display all related rows, or related rows where VisibleToAll = true.

This code, feels like it should be in the controller, but I'm not sure how it would look, because as it stands, the controller (limmited version) looks like this:

return View("TableADetailView", repos.GetTableA(id));

Would something like this be even work, and would it be bad what if my DataContext gets submitted, would that delete all the rows that have VisibleToAll == false?

var tblA = repos.GetTableA(id);
tblA.TableAChild1 = tblA.TableAChild1.Where(tmp => tmp.VisibleToAll == true);
tblA.TableAChild2 = tblA.TableAChild2.Where(tmp => tmp.VisibleToAll == true);
return View("TableADetailView", tblA);

It would also be simple to add that logic to the RendarPartial call from the main view:

<% Html.RenderPartial("TableAChild1", Model.TableAChild1.Where(tmp => tmp.VisibleToAll == true); %>

© Stack Overflow or respective owner

Related posts about linq-to-sql

Related posts about asp.net-mvc