1

i am learning asp.net mvc 2 but i have a problem that i can pass the anonymous type to the view. Here is the code :

DBDataContext db = new DBDataContext();
        var gca = from x in db.tbl_controllers
                  select new
                  {
                      x,
                      tblAction = (from y in db.tbl_actions
                                   select new
                                   {
                                       y,
                                       visible = (from z in db.tbl_controller_actions
                                                  where z.actionId == x.id && z.controllerId == y.id
                                                  select z).Single<tbl_controller_action>() == null ? false : true,
                                       isExisted = (from t in db.tbl_group_controller_actions
                                                    where t.groupId == id && t.controllerId == x.id && t.actionId == y.id
                                                    select t).Single<tbl_group_controller_action>() == null ? false : true
                                   }),
                  };

How can I pass the gca to the view ? Thank you very much.

hieund
  • 11
  • 1

2 Answers2

0

Assuming the above is in the controller

at the end you can write

return View(gca);

Edit

Since the return type is a list you view will inherit like

Inherits="System.Web.Mvc.ViewPage<IEnumerable<TestMVC4.Models.tbl_controllers>>"

and in your view you can parse each item as

<% foreach (var item in Model)
   { 
..
item.<yourproperty>
..
}
%>
ajay_whiz
  • 17,573
  • 4
  • 36
  • 44
  • but what is the strong-type view ? <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master" Inherits="System.Web.Mvc.ViewPage????>" %> and how can i get the value from Model ? – hieund Aug 25 '10 at 10:18
  • @Hieund Strongly typed view insists the type you return from the controller should match the type of your view. e.g if your view Inherits="System.Web.Mvc.ViewPage" then your controller should return MyModel – ajay_whiz Aug 25 '10 at 10:28
  • @hieund the easy way to generate a view for your model is right click on your controller action and select 'Add View' option. – ajay_whiz Aug 25 '10 at 10:30
  • I know how to generate the View but the problem is that i cannot get the data in Model. If you can, could you show me what the View looks like ? What exactly the MyModel in System.Web.Mvc.ViewPage is ? Here is my another Model
    System.Web.Mvc.ViewPage>
    – hieund Aug 25 '10 at 15:16
  • Thanks. But when you use "TestMVC4.Models.tbl_controllers", you cannot get the tblAction, visible , isExisted value. That is my problem. – hieund Aug 25 '10 at 16:06
  • @hieund looking and your linq statement if you use tbl_controllers then in view page you can get tblAction as item.tblAction in your foreach statement – ajay_whiz Aug 26 '10 at 07:37
0

I've solved my question. But It may be not the best answer : I created an object to return from controller :

public class GroupViewDetail
{
    public tbl_group GroupDB { get; set; }
    public List<GroupViewRow> GVRow { get; set; }
    public List<tbl_action> ListActionDB { get; set; }
    public class GroupViewRow
    {
        public tbl_controller ControllerDB { get; set; }
        public List<SubGroupViewRow> SubGVRow { get; set; }
        public class SubGroupViewRow
        {
            public tbl_action ActionDB { get; set; }
            public bool IsVisible { get; set; }
            public bool IsExisted { get; set; }
        }
    }
}

Then, in controller, I do this :

DBDataContext db = new DBDataContext();
        GroupViewDetail gvd = new GroupViewDetail();
        gvd.GroupDB = db.tbl_groups.Single<tbl_group>(g => g.id == id);
        gvd.ListActionDB = (from a in db.tbl_actions select a).DefaultIfEmpty<tbl_action>().ToList<tbl_action>();
        gvd.GVRow = new List<GroupViewDetail.GroupViewRow>();
        foreach (tbl_controller c in db.tbl_controllers)
        {
            GroupViewDetail.GroupViewRow gvr = new GroupViewDetail.GroupViewRow();
            gvr.ControllerDB = c;
            gvr.SubGVRow = new List<GroupViewDetail.GroupViewRow.SubGroupViewRow>();
            foreach (tbl_action a in db.tbl_actions)
            {
                GroupViewDetail.GroupViewRow.SubGroupViewRow sgvr = new GroupViewDetail.GroupViewRow.SubGroupViewRow();
                sgvr.ActionDB = a;
                sgvr.IsExisted = (from t in db.tbl_group_controller_actions
                                  where t.groupId == id && t.controllerId == c.id && t.actionId == a.id
                                  select t).DefaultIfEmpty<tbl_group_controller_action>().Single<tbl_group_controller_action>() == null ? false : true;
                sgvr.IsVisible = (from z in db.tbl_controller_actions
                                  where z.actionId == a.id && z.controllerId == c.id
                                  select z).DefaultIfEmpty<tbl_controller_action>().Single<tbl_controller_action>() == null ? false : true;
                gvr.SubGVRow.Add(sgvr);
            }
            gvd.GVRow.Add(gvr);
        }
        return View(gvd);

finally, this is the view :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/MasterPage.Master"
Inherits="System.Web.Mvc.ViewPage<TestMVC4.Models.Group.GroupViewDetail>" %>
hieund
  • 11
  • 1