将两个ViewModel,一个DropDownList和WebGrid组合到一个视图中

时间:2012-10-05 11:08:39

标签: asp.net-mvc viewmodel webgrid html.dropdownlistfor

我需要一些关于如何继续使用MVC4 Web应用程序的建议。

目前,我有两个控制器方法,一个用于显示DropDownList,另一个用于显示WebGrid:

    [HttpGet]
    public ActionResult Practice(TestTypesViewModel model, int Parent_ID = 45875)
    {
        var query = (from u in db.Users
                     join ur in db.User_Relationship on u.User_ID equals ur.Child_ID
                     join ub in db.Tbl_Admin_User_Batch on u.User_ID equals ub.User_Id

                     join ut in db.User_Tests on u.User_ID equals ut.User_ID into ps
                     from ut in ps.DefaultIfEmpty()

                     join lu in db.Lookups on u.First_LanguageID equals lu.LookupID

                     where ur.Parent_ID == Parent_ID && ub.Batch_Id == model.Batch_ID

                     group new { u, lu, ut } by new
                     {
                         u.User_ID,
                         u.Forename,
                         u.Surname,
                         u.Client_Code,
                         u.User_Name,
                         u.Password,
                         u.Email,
                         u.Gender,
                         u.Report_date,
                         u.EmailDate,
                         u.Job_Function,
                         lu.LookupValue
                     } into g

                     select new UserViewModel
                     {
                         User_ID = g.Key.User_ID,
                         Forename = g.Key.Forename,
                         Surname = g.Key.Surname,
                         Client_Code = g.Key.Client_Code,
                         User_Name = g.Key.User_Name,
                         Password = g.Key.Password,
                         Email = g.Key.Email,
                         Gender = g.Key.Gender,
                         Report_Date = g.Key.Report_date,
                         Email_Date = g.Key.EmailDate,
                         Test_Count = g.Count(x => x.ut.Test_ID != null),
                         Test_Completed = g.Count(x => x.ut.Completed != null),
                         Job_Function = g.Key.Job_Function,
                         Lookup_Value = g.Key.LookupValue
                     }).ToList();

        return View(query);
    }

...

public ActionResult SelectCategory()
    {

        var query = (from ab in db.Tbl_Admin_Batch
                     from ub in db.Tbl_Admin_User_Batch
                     where ub.User_Id == 45875 && ab.Batch_Id == ub.Batch_Id
                     select ab).ToList();

        model.Test_Types = query.Select(c => new SelectListItem
        {
            Text = c.Batch_Name,
            Value = c.Batch_Id.ToString()
        }).ToList();

        return View(model);
    }

每个都有一个ViewModel和相应的视图,见下文:

@model HFI_Assessment_Administration.ViewModels.TestTypesViewModel

@using (Html.BeginForm("Practice", "WebFormUserList", FormMethod.Get))
{
   @Html.DropDownListFor(model => model.Batch_ID, Model.Test_Types)
   <input type="submit" />
}

...

@model IEnumerable<HFI_Assessment_Administration.ViewModels.UserViewModel>

<div>
@{
    var grid = new WebGrid(Model);
}

@grid.GetHtml(

//Looks at CSS for .webgrid
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",  
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style",

columns: grid.Columns
    (
    grid.Column(columnName:"User_ID", header: "User ID", style: "text-align-center"),
    grid.Column(columnName:"Forename", header: "Forename", style: "text-align-center"),
    grid.Column(columnName:"Surname", header: "Surname", style: "text-align-center"),
    grid.Column(columnName:"Client_Code", header: "Client Code", style: "text-align-center"),
    grid.Column(columnName:"User_Name", header: "User Name", style: "text-align-center"),
    grid.Column(columnName:"Password", header: "Password", style: "text-align-center"),
    grid.Column(columnName:"Email", header: "Email", style: "text-align-center"),
    grid.Column(columnName:"Gender", header: "Gender", style: "text-align-center"),
    grid.Column(columnName:"Report_Date", header: "Report Date", style: "text-align-center"),
    grid.Column(columnName:"Email_Date", header: "Email Date", style: "text-align-center"),
    grid.Column(columnName:"Test_Count", header: "Count", style: "text-align-center"),
    grid.Column(columnName:"Test_Completed", header: "Completed", style: "text-align-center"),
    grid.Column(columnName:"Job_Function", header: "Job Function", style: "text-align-center"),
    grid.Column(columnName:"Lookup_Value", header: "Language", style: "text-align-center")
    )          
)
</div>

DropDownList值传递给webgrid并显示数据。问题是,它们是在两个不同的视图上,我希望它们是相同的。

即。您选择一个下拉值,该表将更新数据。我确定这并不困难,但我是MVC的新手,我正在努力保持简单。

我是否只使用其他两个ViewModel的信息创建一个大的ViewModel?

OR

我是否将其他两个ViewModel作为值内的ViewModel创建?

非常感谢!

0 个答案:

没有答案