【问题标题】:Mvc Grid Data BindMvc 网格数据绑定
【发布时间】:2011-03-21 13:18:16
【问题描述】:

我正在使用asp.net mvc3.0 razor框架和html Table在ViewBag的帮助下绑定数据。我必须根据下拉列表中的选定选项显示数据。

public ActionResult Index(GetFileName fn)
{
....
ViewBag.Grid1Data =dt;
return View();

}

第一次,我得到了数据,但是下次当从下拉列表中选择另一个选项来获取第二个表时,该表没有显示,但我正在获取数据。

查看包括 下拉

    <select id="Select2">
        <option>Select</option>
        <option>abc</option>
        <option>des</option>
        <option>fgh</option>
        <option>next</option>
        <option>first</option>
    </select>

// Heading for the table
<h2>@ViewBag.Heads</h2>
   //  Table
 <table id="table" cellpadding="10px" cellspacing="0" class="TableSty">
                @for (int j = 0; j < ViewBag.Grid1Data.Rows.Count; j++)
                {
                    var color = "";
                    if (j % 2 == 0)
                    { color = "#f2f2f2"; }
                    else { color = "white"; }
                    <tr style="background:@color" >
                        @for (int c = 0; c < @ViewBag.Grid1Data.Columns.Count; c++)
                        {
                            <td class="data">@ViewBag.Grid1Data.Rows[j][c].ToString()
                            </td>
                        }

                    </tr>
                }
            </table> 

请帮我解决问题

【问题讨论】:

  • 我们可以看到您的视图,包括任何脚本吗?

标签: javascript asp.net html asp.net-mvc-3


【解决方案1】:

您的选项列表和表格未绑定。最好的方法是使用 Html.DropDownListFor 来呈现一个下拉列表,这样每次从下拉列表中选择时,页面都会刷新,并且您会在 viewbag 中获得更新的数据。

或者,您有一个按钮可以在您选择 DropDown 值后手动提交和刷新页面。

【讨论】:

    猜你喜欢
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    相关资源
    最近更新 更多