Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Dropdown list persting for each requests
    primarykey
    data
    text
    <p>I am working on a web app using MVC, where all pages have header section consiting of three dropdowns one text box and an image button. So no matter on which page user is, he/she has ability to select a combination out of three dropdowns, enter some text in text box and hit image button to search in database.</p> <p>So i am using a strongly typed(BaseViewModel) master _Layoutthought, and then add two partial views on it, one to display user info and another one to display all dropdowns, text box and imaage button. Now, Models i have 3 models for each dropdown option/item and then a BaseViewModel which will have 3 properties as a list of each model and 3 int properties to hold selected item ID. And plan is to use this baseViewModel as parent class for all viewmodels so that i can fullfil my strongly tyed _Layout for each request and responce. OK i am not sure that if i have explained my requirement clearly :(.</p> <p>so here is the code all models:</p> <pre><code> 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; } } } </code></pre> <p>ViewModel</p> <pre><code> namespace XYZNameSpace.ViewModels { public class BaseViewModel : User { public long SelectedManageCompanyID { get; set; } public List&lt;SelectListItem&gt; ManageCompanyList { get; set; } public string SelectedManageCompanyName { get; set; } public long SelectedSuperCompanyID { get; set; } public List&lt;SelectListItem&gt; SuperCompanyList { get; set; } public string SelectedSuperCompnayName { get; set; } public int SelectedSearchOptionID { get; set; } public List&lt;SelectListItem&gt; 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&lt;SearchResult&gt; ResultsList { get; set; } } } </code></pre> <p>Controller</p> <pre><code>public class HomeController : Controller { // // GET: /Home/ public ActionResult Home() { BaseViewModel bVM = new BaseViewModel() { ManageCompanyList = (new List&lt;ManageCompany&gt;() { 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 =&gt; new SelectListItem { Value = x.ManageCompanyID.ToString(), Text = x.ManageCompanyName }).ToList(), SuperCompanyList = (new List&lt;SuperCompany&gt;(){ 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 =&gt; new SelectListItem { Value = y.SuperCompanyID.ToString(), Text = y.SuperCompanyName }).ToList(), SearchOptionsList = (new List&lt;SearchOption&gt;(){ 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 =&gt; 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&lt;SearchResult&gt;(){ new SearchResult() { CompanyName = bVM.SelectedSuperCompanyID.ToString(), SubCompanyName = bVM.SelectedManageCompanyID.ToString(), UserName = "Duh" } }; return View("SearchResults", sRVM); } } </code></pre> <p>Views /Home/Home.cshtml</p> <pre><code>@{ ViewBag.Title = "Home"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;div style="float: left; vertical-align: top;"&gt; &lt;/div&gt; &lt;br /&gt; </code></pre> <p>/Home/SearchResults.cshtml</p> <pre><code>@model XYZNameSpace.ViewModels.SearchResultsViewModel &lt;table width="100%"&gt; &lt;thead&gt; &lt;tr&gt; &lt;td&gt; COMPANY &lt;/td&gt; &lt;td&gt; SUB COMPANY &lt;/td&gt; &lt;td&gt; USER NAME &lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; @foreach (XYZNameSpace.Models.SearchResult sr in Model.ResultsList) { &lt;tr&gt; &lt;td&gt; @sr.CompanyName &lt;/td&gt; &lt;td&gt; @sr.SubCompanyName &lt;/td&gt; &lt;td&gt; @sr.UserName &lt;/td&gt; &lt;/tr&gt; } &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>/Shared/SearchDDLPartialView.cshtml</p> <pre><code> @model XYZNameSpace.ViewModels.BaseViewModel &lt;div&gt; &lt;div style="float: left"&gt; &lt;span class="Label"&gt;Search: &lt;/span&gt; @using (Html.BeginForm("Search", "Home", FormMethod.Post)) { @Html.DropDownListFor(x =&gt; x.SelectedSuperCompanyID, Model.SuperCompanyList) @Html.DropDownListFor(x =&gt; x.SelectedManageCompanyID, Model.ManageCompanyList) @Html.DropDownListFor(x =&gt; x.SelectedSearchOptionID, Model.SearchOptionsList) &lt;input type="image" src="../../Content/images/search.gif" /&gt; } &lt;/div&gt; &lt;/div&gt; </code></pre> <p>/Shared/_Layout.cshtml</p> <pre><code>@model XYZNameSpace.ViewModels.BaseViewModel &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;@ViewBag.Title&lt;/title&gt; &lt;link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /&gt; &lt;link href="@Url.Content("~/Content/BEClientProfile.css")" rel="Stylesheet" type="text/css" /&gt; &lt;script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;img src="../../Content/images/logo124.gif" alt="Logo" /&gt; &lt;/td&gt; &lt;td valign="top"&gt; &lt;div style="float: left; vertical-align: top; text-align: left"&gt; &lt;img src="../../Content/images/user-info.gif" alt="User Infor" /&gt; &lt;span class="Label"&gt;Welcome &lt;/span&gt;&lt;span class="HeaderLabelText"&gt;@Model.UserLevel&lt;/span&gt; &lt;/div&gt; &lt;div&gt; &lt;div style="float: left; width: 100%;"&gt; &lt;u&gt;&lt;span class="Label"&gt;Currently Editing:&lt;/span&gt; &lt;/u&gt; &lt;/div&gt; &lt;div style="float: left; width: 100%;"&gt; &lt;span class="Label"&gt;Company:&lt;/span&gt; &lt;span class="HeaderLabelText"&gt;@Model.SelectedManageCompanyID&lt;/span&gt; &lt;/div&gt; &lt;div style="float: left; width: 100%;"&gt; &lt;span class="Label"&gt;User:&lt;/span&gt; &lt;span class="HeaderLabelText"&gt;@Model.UserLoginName&lt;/span&gt; &lt;/div&gt; &lt;/div&gt; &lt;/td&gt; &lt;td&gt; &lt;/td&gt; &lt;td&gt; @{Html.RenderPartial("SearchDDLPartialViews", Model);} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="4"&gt; @RenderBody() &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>/Shared/UserPartialView.cshtml</p> <pre><code>@model XYZNameSpace.Models.User &lt;div style="float: left; margin-left: .5em;"&gt; &lt;img src="../../Content/images/user-info.gif" alt="User Infor" /&gt; &lt;span style="font-weight: bold; float: left;"&gt;Welcome &lt;/span&gt; &lt;/div&gt; </code></pre> <p>OK, i was able to select one item from each dropdown but when i click on image button at search Action in home control, the BaseViewModel has nothing other than selectedIDs, so when action is trying to display SearchResults view back, them _Layout.cshtml throughs error saying there nothing in lists that are used by dropdowns.</p> <p>so how do we persist this information for every request...?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload