2

我正在使用 MVC 开发一个 Web 应用程序,其中所有页面都有标题部分,由三个下拉列表、一个文本框和一个图像按钮组成。所以无论用户在哪个页面上,他/她都可以从三个下拉列表中选择一个组合,在文本框中输入一些文本,然后点击图像按钮在数据库中搜索。

所以我使用了一个强类型(BaseViewModel)master _Layoutthought,然后在上面添加两个部分视图,一个显示用户信息,另一个显示所有下拉列表、文本框和图像按钮。现在,模型我有每个下拉选项/项目的 3 个模型,然后是一个 BaseViewModel,它将有 3 个属性作为每个模型的列表和 3 个 int 属性来保存选定的项目 ID。并且计划使用这个 baseViewModel 作为所有视图模型的父类,这样我就可以为每个请求和响应完成我的强绑定 _Layout。好的,我不确定我是否清楚地解释了我的要求:(。

所以这是所有型号的代码:

    namespace XYZNameSpace.Models
{
    public class ManageCompany
    {
        public long ManageCompanyID { get; set; }

        public string ManageCompanyName { get; set; }
    }
}

    namespace XYZNameSpace.Models
{
    public class SuperCompany
    {
        public long SuperCompanyID { get; set; }

        public string SuperCompanyName { get; set; }
    }
}

    public class User
{
    public long UserID { get; set; }

    public string UserName { get; set; }

    public string UserLoginName { get; set; }

    public string UserLevel { get; set; }

    public int UserLevelId { get; set; }
}

    namespace XYZNameSpace.Models
{
    public class SearchResult
    {
        public string CompanyName { get; set; }

        public string SubCompanyName { get; set; }

        public string UserName { get; set; }
    }
}

视图模型

    namespace XYZNameSpace.ViewModels
{
    public class BaseViewModel : User
    {
        public long SelectedManageCompanyID { get; set; }

        public List<SelectListItem> ManageCompanyList { get; set; }

        public string SelectedManageCompanyName { get; set; }

        public long SelectedSuperCompanyID { get; set; }

        public List<SelectListItem> SuperCompanyList { get; set; }

        public string SelectedSuperCompnayName { get; set; }

        public int SelectedSearchOptionID { get; set; }

        public List<SelectListItem> SearchOptionsList { get; set; }

        public string SelectedSearchOptionName { get; set; }

        public string SearchWord { get; set; }
    }

    public class SearchOption
    {
        public int SearchOptionID { get; set; }

        public string SearchOptionName { get; set; }
    }
}

    namespace XYZNameSpace.ViewModels
{
    public class SearchResultsViewModel : BaseViewModel
    {
        public List<SearchResult> ResultsList { get; set; }
    }
}

控制器

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Home()
    {
        BaseViewModel bVM = new BaseViewModel()
        {
            ManageCompanyList = (new List<ManageCompany>() {
                new ManageCompany()
                {
                    ManageCompanyID = 1,
                    ManageCompanyName = "MC 1"
                },
                                    new ManageCompany()
                {
                    ManageCompanyID = 2,
                    ManageCompanyName = "MC 2"
                },
                                    new ManageCompany()
                {
                    ManageCompanyID = 3,
                    ManageCompanyName = "MC 3"
                },
                                    new ManageCompany()
                {
                    ManageCompanyID = 4,
                    ManageCompanyName = "MC 4"
                },
                                    new ManageCompany()
                {
                    ManageCompanyID = 5,
                    ManageCompanyName = "MC 5"
                }
             }).Select(x => new SelectListItem { Value = x.ManageCompanyID.ToString(), Text = x.ManageCompanyName }).ToList(),
            SuperCompanyList = (new List<SuperCompany>(){
             new SuperCompany(){
                SuperCompanyID=6,
                SuperCompanyName="SC 6"
             },
             new SuperCompany(){
                SuperCompanyID=7,
                SuperCompanyName="SC 7"
             },
             new SuperCompany(){
                SuperCompanyID=8,
                SuperCompanyName="SC 8"
             },
             new SuperCompany(){
                SuperCompanyID=9,
                SuperCompanyName="SC 9"
             },
             new SuperCompany(){
                SuperCompanyID=10,
                SuperCompanyName="SC 10"
             }
             }).Select(y => new SelectListItem { Value = y.SuperCompanyID.ToString(), Text = y.SuperCompanyName }).ToList(),

            SearchOptionsList = (new List<SearchOption>(){
             new SearchOption(){
                SearchOptionID=11,
                SearchOptionName="SO 11"
             },
             new SearchOption(){
                SearchOptionID=12,
                SearchOptionName="SO 12"
             },
             new SearchOption(){
                SearchOptionID=13,
                SearchOptionName="SO 13"
             },
             new SearchOption(){
                SearchOptionID=14,
                SearchOptionName="SO 14"
             },
             new SearchOption(){
                SearchOptionID=15,
                SearchOptionName="SO 15"
             }
             }).Select(y => new SelectListItem { Value = y.SearchOptionID.ToString(), Text = y.SearchOptionName }).ToList()
        };

        return View(bVM);
    }

    [HttpPost]
    public ActionResult Search(BaseViewModel bVM)
    {
        SearchResultsViewModel sRVM = new SearchResultsViewModel();
        sRVM.ResultsList = new List<SearchResult>(){
            new SearchResult()
            {
             CompanyName = bVM.SelectedSuperCompanyID.ToString(),
             SubCompanyName = bVM.SelectedManageCompanyID.ToString(),
             UserName = "Duh"
            }
        };
        return View("SearchResults", sRVM);
    }
}

视图 /Home/Home.cshtml

@{
    ViewBag.Title = "Home";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div style="float: left; vertical-align: top;">

</div>
<br />

/Home/SearchResults.cshtml

@model XYZNameSpace.ViewModels.SearchResultsViewModel
<table width="100%">
    <thead>
        <tr>
            <td>
                COMPANY
            </td>
            <td>
                SUB COMPANY
            </td>
            <td>
                USER NAME
            </td>
        </tr>
    </thead>
    <tbody>
        @foreach (XYZNameSpace.Models.SearchResult sr in Model.ResultsList)
        {
            <tr>
                <td>
                    @sr.CompanyName
                </td>
                <td>
                    @sr.SubCompanyName
                </td>
                <td>
                    @sr.UserName
                </td>
            </tr>
        }
    </tbody>
</table>

/Shared/SearchDDLPartialView.cshtml

    @model XYZNameSpace.ViewModels.BaseViewModel
<div>
    <div style="float: left">
        <span class="Label">Search: </span>
        @using (Html.BeginForm("Search", "Home", FormMethod.Post))
        {
            @Html.DropDownListFor(x => x.SelectedSuperCompanyID, Model.SuperCompanyList)
            @Html.DropDownListFor(x => x.SelectedManageCompanyID, Model.ManageCompanyList)
            @Html.DropDownListFor(x => x.SelectedSearchOptionID, Model.SearchOptionsList)
            <input type="image" src="../../Content/images/search.gif" />

        }
    </div>
</div>

/Shared/_Layout.cshtml

@model XYZNameSpace.ViewModels.BaseViewModel
<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/BEClientProfile.css")" rel="Stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
</head>
<body>
    <table>
        <tr>
            <td>
                <img src="../../Content/images/logo124.gif" alt="Logo" />
            </td>
            <td valign="top">
                <div style="float: left; vertical-align: top; text-align: left">
                    <img src="../../Content/images/user-info.gif" alt="User Infor" />
                    <span class="Label">Welcome </span><span class="HeaderLabelText">@Model.UserLevel</span>
                </div>
                <div>
                    <div style="float: left; width: 100%;">
                        <u><span class="Label">Currently Editing:</span> </u>
                    </div>
                    <div style="float: left; width: 100%;">
                        <span class="Label">Company:</span> <span class="HeaderLabelText">@Model.SelectedManageCompanyID</span>
                    </div>
                    <div style="float: left; width: 100%;">
                        <span class="Label">User:</span> <span class="HeaderLabelText">@Model.UserLoginName</span>
                    </div>
                </div>
            </td>
            <td>
            </td>
            <td>
                @{Html.RenderPartial("SearchDDLPartialViews", Model);}
            </td>
        </tr>
        <tr>
            <td colspan="4">
                @RenderBody()
            </td>
        </tr>
    </table>
</body>
</html>

/Shared/UserPartialView.cshtml

@model XYZNameSpace.Models.User
<div style="float: left; margin-left: .5em;">
    <img src="../../Content/images/user-info.gif" alt="User Infor" />
    <span style="font-weight: bold; float: left;">Welcome </span>
</div>

好的,我能够从每个下拉列表中选择一个项目,但是当我在主页控件中的搜索操作中单击图像按钮时,BaseViewModel 除了 selectedIDs 之外什么都没有,所以当操作试图显示 SearchResults 视图时,它们会通过 _Layout.cshtml错误说下拉列表使用的列表中没有任何内容。

那么我们如何为每个请求保留这些信息......?

4

1 回答 1

1

快速解决方案是将 List < SelectListItem > 或视图模型本身存储在会话中,并在每次返回视图模型时将它们重新引入视图。但是,对于基于云的解决方案,概念相同,但您需要使用其他缓存存储,例如 appfabric、memcache 或 PaaS 允许的其他插件。

于 2012-04-08T02:48:40.233 回答