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!