我正在使用 mvc 2010 Express 在 c#中创建一个应用程序,我有一个带有下拉列表的视图来选择代码。它会将用户重定向到另一个视图(Filter
视图),并使用另一个下拉列表选择市场。
然后,根据用户选择的代码和市场,它会显示该代码和市场所存在的所有国家/地区的表格。 (一个市场有许多国家,每年每个国家至少有一次分析(由代码定义)。)
我在Filter
视图中添加了隐藏元素:
@{
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
控制者:
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);
}
然后在下一个视图(FilterMarket
)中,我使用WebGrid
来显示国家/地区:
@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]
})
问题在用户尝试显示表格的其他页面时出现,因为它显示空白表格,因为它不记得此隐藏元素( code
)。
我想我应该把它添加到我的WegGrid中,但我不知道在哪里。
如果你能帮助我,我将非常感激。我真的需要完成这个。提前谢谢!
答案 0 :(得分:0)
在Controller
和Filter
之间的FilterMarket
中创建中间ActionResult可以解决问题。它将从IdAnalysis
获取FilterMarket
,然后在Code
Market
和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);
}