Pergunta

I am creating an application in c# with mvc 2010 Express and I have one view made from 3 different tables: tmarkets, tcountries and tanalysis. Each market has many countries and each country can have more than one analysis (defined by a code).

I have a view to select the analysis to consult (It passes the code to the controller) and then in the next view I have a drop down list of different markets (So that the user can see the analysis of the countries of that market for that analysis). I need to have two different views because I also need to have other options in the second one.

Inform Controller:

public ActionResult Filter(string 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(), "Code", "Market");
    return View();
}

View Filter:

<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")

}

In order to populate the drop down list with the different markets, my consult selects them from the code and the market itself so that later I can pass the code of the analysis to the controller for the next view.

The problem is that I would also need to pass the market to the controller and not only to the user in the drop down list. I tried to do something like this:

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

and then in my controller:

var idAnalysis = (from v in dre.v_AnalysisCountry
                  where v.Code == id
                  select v.IdAnalysisCountry).FirstOrDefault();
ViewBag.idAn = idAnalysis;

But its not working.

----------------------------------------------------------EDIT------------------------------------------------------------------

I added a hidden attribute in my view:

@{
    string code= ViewBag.code;
}

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

And it works, as it passes the code to the next view. The problem here comes in the next view. I am using a WebGrid to display the countries.

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

When the user tries to display the second page of the table, it displays a blank table, as it doesn't remember this hidden attribute.

I would be very grateful if you could help me with another solution or fixing this partial solution I found. Thanks in advance!

Foi útil?

Solução

Finally I solved the problem with the hidden attribute by adding one intermediate ActionResult (IntermFilter) in my Controller.

It receives the Market and the Code and obtains the Id of the first analysis that fulfills those two parameters. Then it redirects to a view in which the WebGrid is rendered as before, as in the FilterMarket ActionResult it obtains the Market and Code from the IdAnalysis.

Here is my code:

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("FilterAnMark", new { id = idFirstAn });
}

public ActionResult FilterAnMark(int id)
{
    var query = (from m in dre.v_AnalysisCountry
                 where m.IdAnalysis == id
                 select m).FirstOrDefault();
    var countries = new FilterManager().ObtainMarkets(query.Code, query.Market);
    ViewBag.code = query.Code;
    ViewBag.market = query.Market;
    return View(countries);
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top