First of all sorry for the super generic title but i have no idea how to ask this question properly.
I am a bit new to Asp net MVC 5 and i am having some issues with passing information from the Controller to the View. The problem is that in the View I need to have 2 search functions, one using a text (with a textbox) and another with listboxes, and I can t seem to get the two working at the same time (they work separately), I suspect it has something to do with my Viewmodel but I have no idea why this is happening.
this is my ViewModel:
public class ViewModel{
public IEnumerable<ABC> xyz { get; set; }
public List<SelectListItem> device { get; set; }
public List<SelectListItem> type { get; set; }
public SelectListItem selectedItem { set; get; }
//these are all the properties that ABC has
public int a { get; set; }
public string Name{ get; set; }
}
These are the controller actions
public ActionResult FillListBoxes()
{
ViewModel myViewModel = new ViewModel();
var tempObjects = db.ABC
//.Where(...)
.Select(v =>
new
{
v.Type,
v.device,
})
.ToList();
myViewModel.device = new List<SelectListItem>();
foreach (var device in tempObjects.Select(obj => obj.device).Distinct().ToList())
{
myViewModel.device.Add(new SelectListItem { Text = device, Value = device });
}
myViewModel.type = new List<SelectListItem>();
foreach (var type in tempObjects.Select(obj => obj.device).Distinct().ToList())
{
myViewModel.type.Add(new SelectListItem { Text = type, Value = type });
}
return View(myViewModel);
}
public ActionResult Search(string searchString)
{
ViewModel myViewModel = new ViewModel();
// FillListBoxes();
var tempObjects = db.ABC.ToList();
if (!String.IsNullOrEmpty(searchString))
{
myViewModel.abc = tempObjects.Where(m => m.Name.Contains(searchString));
return View(myViewModel);
}
else
{
myViewModel.abc = tempObjects;
return View(myViewModel);
}
}
And this is the View
@model project.Models.ViewModel
@{
ViewBag.Title = "Search";
}
@section Styles {
<link href="@Url.Content("~/Content/Search.css")" rel="stylesheet" type="text/css" />
<link href="@Url.Content("~/Content/chosen.css")" rel="stylesheet" type="text/css" />
}
@using (Html.BeginForm("Search", "ABC", FormMethod.Get))
{
<p>
Name: @Html.TextBox("SearchString") <br />
<input type="submit" value="Filter" />
</p>
}
@using (Html.BeginForm("FillListBoxes", "ABC", FormMethod.Get))
{
<p>
@Html.DropDownListFor(model => model.selectedItem, Model.device, new { @class = "chosen", multiple = "multiple", style = "width: 350px;" })
</p>
<p>
@Html.DropDownListFor(model => model.selectedItem, Model.type, new { @class = "chosen", multiple = "multiple", style = "width: 350px;" })
</p>
<p>
<input type="submit" value="Filter" />
</p>
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.a)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th></th>
</tr>
@foreach (var item in Model.abc) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.a)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
</tr>
}
</table>
The error i am getting now is that selectedItem should be a IEnumerable but even if i change it get a There is no ViewData item of type 'IEnumerable' that has the key 'selectedItem'. and before this I was facing a: the model item passed into the dictionary is of type 'system.data.entity.infrastructure.dbquery 1[abc] but this dictionary requires a model item of type ViewModel this is when I changed the Search method in the controller to use the Viewmodel.
If I am missing anything please do say.
I apologize again for the super generic title.
@Edit Added the suggestions made by Stephen
@Edit 2 So here is a brief explanation on how this all works, the texbox is there so the user can filter the ABCs that are presented in that page (based solely on the name attribute). The listboxes are there so if the user can filter the presented ABCs through their type and/or device only.
I hope this clears some things up
ABC's based on theNameproperty? What are the dropdownlists doing - arethey also filteringABC's based on other properties and if so, which?