MVC Partial view with controller, ajax - how do I get a partial controller to receive data?

I am learning MVC and am confused by this. I am trying to use some common code that receives data and displays it in a WebGrid on a partial view that I can use on multiple pages.

My home controller The index method simply returns the view (). The Home view is as follows:

@using (Ajax.BeginForm("SearchAction", "Search",
    new AjaxOptions { UpdateTargetId = "data-grid", HttpMethod = "Post" }))
{
    @Html.TextBoxFor(model => model.name)
    <input type="submit" value="Search" />
}

@{
<div id="data-grid">
    @Html.Partial("SearchResults", Model)
</div>
}

I am trying to use Ajax so as not to lose the search form data when I click on a link to a WebGrid pager that displays as regular links.

My SearchController is as follows:

public ActionResult SearchAction(string name)
{
    return RedirectToAction("SearchResults", new { name = name });
}

public ActionResult SearchResults(string name)
{
    //does database query and sticks results in the viewbag
    //filter on optional name parameter

    VieweBag.Members = MyQueryResults;
    return PartialView();
}

My sharing view is SearchResults, data is passed through ViewBag.Members:

@{
    var grid = new WebGrid(null, rowsPerPage: ViewBag.Pagesize);
    grid.Bind(ViewBag.Members);
    @grid.GetHtml(// etc. etc.)
}

, , , ViewBag.Pagesize ViewBag.Members , . , ViewBag, . ?

, ( ), , , , view . , , .

/ , , - :\

+1
2

, . , , viewdata. , , (, ):)

Update:

GET , Html.Action. :

, , , .

+2

Jonass , ViewBag .

, , - , , ViewBag.

, , , MyQueryResults :

IEnumerable<Result>

@Model IEnumerable<Result>

Render:

@Html.Partial("SearchResults", ViewBag.Members);

, , , .

!

0

All Articles