0

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:

  1. Is it possible to make the ActionLink perform a submit on the form ones clicked?
  2. 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>
}
Casper Nybroe
  • 1,179
  • 3
  • 23
  • 47
  • Which version of MVC are you using? Also, I'm assuming search and searchBy should be populated by user input (e.g. stuff typed in a textbox)? If so, take a look [here](http://stackoverflow.com/questions/16459598/how-to-send-textbox-value-to-actionlink-in-asp-net-mvc), [here](http://stackoverflow.com/questions/11293048/how-to-pass-textbox-value-using-html-actionlink-in-asp-net-mvc-3) and [here](http://stackoverflow.com/questions/1856518/passing-textbox-value-using-html-actionlink) for further information. – Esoteric Screen Name Mar 10 '15 at 11:39
  • Thank you for your answer. I am using MVC5 and your assumption is correct. search is an textbox and searchBy is Radiobuttons. Thank you! I will take a look at the solutions you linket to. – Casper Nybroe Mar 10 '15 at 11:46
  • I have testet each of the suggestions in your links and they doesnt seem to work unfortunately. – Casper Nybroe Mar 10 '15 at 13:04

1 Answers1

0

1. You can use JavaScript in your action link to perform a submit:

@Html.ActionLink("Tekst", "Index", new { 
     sortOrder = ViewBag.TextSortParm,  
     onclick = "document.getElementById('<your form id>').submit();"
})

Don't forget to add an Id () to your form.

  1. no.
Yann Olaf
  • 597
  • 3
  • 12
  • Hi Yann, sorry the late reply! I tried to set it up as you mention but my search string is still null. I have edited my question so it contains your solution. Am I missing something? – Casper Nybroe Mar 10 '15 at 11:06