Question

I am creating an application in c# with mvc 2010 Express and I have one view with a drop down list to select a code. It redirects the user to another view (Filter view) with another drop down list to select a market.

Then, depending on which code and market the user has selected, it displays a table of all the countries that exist for that code and market. (One market has many countries and there is at least one analysis (defined by a code) for each country per year).

I added a hidden element in my Filter view:

@{
    string code= ViewBag.code;
}

<script type="text/javascript">
$(function () {
    $("#Market").change(function () {
    var actionUrl = $('#TheForm1').attr('action') + '/' + $('#Market').val();
        $('#TheForm1').attr('action', actionUrl);
        $('#TheForm1').submit();
    });
});
</script>

@using (Html.BeginForm("FilterAnMark", "Inform", FormMethod.Post, new { id = "TheForm1"})){
    @Html.DropDownList("Market", (SelectList)ViewBag.mar, "Select a market")
    @Html.Hidden("code", code)
}

Inform Controller:

public ActionResult Filter(string id)
{
    ViewBag.code = id;
    var merc = from a in dre.v_AnalysisCountry
               where a.Code == id
               select a;
    var query1 = merc.Select (a => new { a.Code,a.Market}).Distinct().OrderBy(a => a.Market);
    ViewBag.mar = new SelectList(query1.AsEnumerable(), "Market", "Market");
    return View();
}

public ActionResult FilterMarket(string id, string code)
{
    var countries = new FilterManager().ObtainAnMarket(code, id);
    return View(countries);
}

And then in this next view (FilterMarket) I use a WebGrid to display the countries:

@model IEnumerable<AlianzasIntranet.Models.DB.v_AnalysisCountry>

@{
    WebGrid grid = new WebGrid(Model, defaultSort: "Market", rowsPerPage: 15);
}

@grid.GetHtml(
    fillEmptyRows: true,
    mode: WebGridPagerModes.All,
    firstText: "<< First",
    previousText: "< Previous",
    nextText: "Next >",
    lastText: "Last>>",

    columns: new[] {
        grid.Column("CountryName", style: "direc", header: "Country"),
        ... [etc]
})

The problem comes when the user tries to display another page of the table, as it displays a blank table because it doesn't remember this hidden element (the code). I think I should add it somewhere in my WegGrid but I don't know where.

I would be very grateful if you could help me. I really need to finish this. Thanks in advance!

Was it helpful?

Solution

Creating an intermediate ActionResult in the Controller between Filter and FilterMarket solves the problem. It will obtain the IdAnalysis and then in FilterMarket the Code and Market from that IdAnalysis:

public ActionResult Filter(string id)
{
    ViewBag.code = id;
    var merc = from a in dre.v_AnalysisCountry
               where a.Code == id
               select a;
    var query1 = merc.Select (a => new { a.Code,a.Market}).Distinct().OrderBy(a => a.Market);
    ViewBag.mar = new SelectList(query1.AsEnumerable(), "Market", "Market");
    return View();
}

public ActionResult IntermFilter(string id, string code)
{
    int idFirstAn = (from m in dre.v_AnalysisCountry
                     where m.Code == code && m.Market == id
                     select m.IdAnalysis).FirstOrDefault();
    return RedirectToAction("FilterMarket", new { id = idFirstAn });
}

public ActionResult FilterMarket(int id)
{
    var query = (from m in dre.v_AnalysisCountry
                 where m.IdAnalysis == id
                 select m).FirstOrDefault();
    var countries = new FilterManager().ObtainAnMarket(query.Code, query.Market);
    return View(countries);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top