I am trying to pull two variables, one from a TextBox("search") and one from a RadioButton("searchBy"), and pull them into the controller when clicking the ActionLink.
When I use the search form with the submit button the "search" and "searchBy" strings are pulled into the controller. But if I click one of the ActionLinks I only get the "sortOrder" string. I assume it is because the ActionLink isn´t doing a submit?
So I figure there is two ways to fix it and therefore my two question:
- Is it possible to make the ActionLink perform a submit on the form ones clicked?
- Is it possible to pull the "search" and "searchBy" string with the ActionLink?
My HOME controller:
public ActionResult Index(string searchBy, string search, string sortOrder)
{
ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
ViewBag.TextSortParm = String.IsNullOrEmpty(sortOrder) ? "text_desc" : "";
ViewBag.PriceSortParm = sortOrder == "Price" ? "price_desc" : "Price";
ViewBag.CubicMeterSortParm = sortOrder == "CubicMeter" ? "cubicMeter_desc" : "CubicMeter";
ViewBag.PricePerCubicSortParm = sortOrder == "PricePerCubic" ? "pricePerCubic_desc" : "PricePerCubic";
ViewBag.ConstructionTypeSortParm = sortOrder == "constructionType" ? "constructionType_desc" : "constructionType";
var text = from s in db.LearningNumbers select s;
if (!String.IsNullOrEmpty(search))
{
if (searchBy == "Tekst")
{
text = text.Where(x => x.Note.Contains(search) || search == null);
}
else
{
text = text.Where(x => x.Name.Contains(search) || search == null);
}
}
switch (sortOrder)
{
case "name_desc":
if (searchBy == "Tekst")
{
text = text.OrderByDescending(s => s.Name).Where(x => x.Note.Contains(search) || search == null);
}
else
{
text = text.OrderByDescending(s => s.Name).Where(x => x.Name.Contains(search) || search == null);
}
break;
case "text_desc":
if (searchBy == "Tekst")
{
text = text.OrderByDescending(s => s.Note).Where(x => x.Note.Contains(search) || search == null);
}
else
{
text = text.OrderByDescending(s => s.Note).Where(x => x.Name.Contains(search) || search == null);
}
}
return View(text.ToList());
}
My Index View:
@using (Html.BeginForm("Index", "Home", FormMethod.Get, new { id = "searchForm" }))
{
<div class="row">
<div class="col-md-12">
<hr />
<h2>Søgeresultater</h2>
@Html.RadioButton("searchBy", "Navn", true) <text>Navn</text>
@Html.RadioButton("searchBy", "Tekst") <text>Tekst</text><br />
@Html.TextBox("search")<input class="btn btn-primary" style="margin:0 10px;" type="submit" value="Søg" />
</div>
<div class="col-md-12 table" style="display:table; margin:45px 0 25px 0;">
<div class="col-md-2">
@*<b>Tekst</b><br />*@
<h4>@Html.ActionLink("Navn", "Index", new { sortOrder = ViewBag.NameSortParm, onclick = "document.getElementById('searchForm').submit();" } })</h4>
</div>
<div class="col-md-2">
@*<b>Tekst</b><br />*@
<h4>
@Html.ActionLink("Tekst", "Index", new { sortOrder = ViewBag.TextSortParm, onclick = "document.getElementById('searchForm').submit();" } })
</h4>
</div>
</div>
</div>
}