Click here to Skip to main content
15,897,226 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir,
I want to make a searchin page with dropdown and I want to show search items on a partial view but when I select dropdown and click search then I do not found the value of dropdown on my controller page .
Posted
Comments
[no name] 1-Feb-16 1:06am    
Tell us clearly, what is present in which file. Means where is your dropdown, button and grid etc are present..
Member 12222029 1-Feb-16 4:14am    
I am use a Index.chtml where we use dropdownlist that is bind from database and my search button is also in this page and within this chtml page I want to use partial.chtml page where I want to show my search result grid but in this case I can't find my dropdown selected value on button click but one thing is also happened here that when I use button submit type then no problem but in this case my page is totaly refresh (it is a problem)

1 solution

Lots of ways to do this, but the gist is that you need to render your partial view from an action that you pass the search term to.

Anyway, he's a rough example

models

C#
public class SearchTerm
{
    public string SearchID { get; set; }
    public string SearchText { get; set; }
}

public class SearchModel
{
    public string SearchValue { get; set; }
    public SelectList SearchTerms { get; set; }
}



index.cshtml

@model Models.SearchModel

@using(Html.BeginForm())
{ 
    <p>
        @Html.DropDownListFor(m => m.SearchValue, Model.SearchTerms)
    </p>
    <p>
        <input type="submit" value="Search"/>
    </p>
}

@if(Model != null)
{
    @Html.Action("Search", "Home", new { searchTerm = Model.SearchValue })
}


_searchResults.cshtml

C#
@model List<string>

@if(Model == null)
{
    return;
}

@foreach(string result in Model)
{
    <div>@result</div>
}


Controller

C#
private SelectList GetTerms()
{
    return new SelectList(new List<SearchTerm>
    {
        new SearchTerm{SearchID="1", SearchText="One"},
        new SearchTerm{SearchID="2", SearchText="Two"},
        new SearchTerm{SearchID="3", SearchText="Three"}
    }, "SearchID", "SearchText");
}

[HttpGet]
public ActionResult Index()
{
    SearchModel model = new SearchModel();
    model.SearchTerms = GetTerms();
    return View(model);
}

[HttpPost]
public ActionResult Index(SearchModel model)
{
    model.SearchTerms = GetTerms();
    return View(model);
}

public ActionResult Search(string searchTerm)
{
    if (string.IsNullOrWhiteSpace(searchTerm))
    {
        return new EmptyResult();
    }

    // do your search here
    List<string> results = new List<string> { "Result 1 for " + searchTerm, "Result 2 for " + searchTerm };
    return PartialView("~/Views/Home/_searchResults.cshtml", results);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900