WebGrid:下一页不记得隐藏元素

时间:2013-09-10 08:10:01

标签: c# jquery asp.net-mvc webgrid hidden-field

我正在使用 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中,但我不知道在哪里。

如果你能帮助我,我将非常感激。我真的需要完成这个。提前谢谢!

1 个答案:

答案 0 :(得分:0)

ControllerFilter之间的FilterMarket中创建中间ActionResult可以解决问题。它将从IdAnalysis获取FilterMarket,然后在Code MarketIdAnalysis中获取:

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);
}