0

当我在视图中删除 webgrid 和 layout=null 时,它的执行和客户端验证消息正在显示...但是当我使用 layout=nlll 和 Webgrid 执行页面时,它显示以下错误“必须先绑定数据源可以执行此操作”

控制器 :

[AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Display()
        {
              return View();
        }

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Display(string brandname)
        {

            ShoppingClass s = new ShoppingClass();

           var ob= s.Searching(brandname);

            return View(ob);

        }

看法 :

@model List<ShoppingCart.Models.ShoppingClass>
@{
    ViewBag.Title = "Display";
      Layout = null;
}

 @{
     var grid = new WebGrid(source: Model, defaultSort: "Drug_Code", rowsPerPage: 20);
}


<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>




        @using (Html.BeginForm(null, null, FormMethod.Post))
        {
            for (int i = 0; i < 1; i++)
            {
      @*
            <table><tr><td> @Html.Label("BrandName")</td>
            <td>@Html.TextBox("BrandName")<div>@Html.ValidationMessage("BrandName")</div></td>
            <td><input type="submit" value="Search" name="Search" /></td></tr></table>*@


            <table><tr><td> @Html.LabelFor(o => o[i].BrandName)</td>
            <td>@Html.TextBoxFor(o => o[i].BrandName)<div>@Html.ValidationMessageFor(o => o[i].BrandName)</div></td>
            <td><input type="submit" value="Search" name="Search" /></td></tr></table>


            }

        }

    <div id="grid">
    @grid.GetHtml(tableStyle: "listing-border", headerStyle: "gridhead", footerStyle: "paging", rowStyle: "td-dark", alternatingRowStyle: "td-light",
       columns: grid.Columns(
                   grid.Column("GenericName", format: @<text>@item.GenericName</text>),
                   grid.Column("BrandName", format: @<text>@item.BrandName</text>),
                   grid.Column("Purchaseqty", format: @<text>@item.Purchaseqty</text>),
                   grid.Column("Purchaseprice", format: @<text>@item.Purchaseprice</text>),
                   grid.Column("Drug_Code", format: @<text>@item.Drug_Code</text>),
                   grid.Column(header: "", format: (item) => Ajax.ActionLink("Add to Cart", "ADDTOCART",
                   new { brandname = @item.BrandName, purchaseqty = @item.Purchaseqty, drugcode = @item.Drug_Code }, new AjaxOptions { HttpMethod = "Post", OnSuccess = "ADDTOCART" }))

                                                                                                )
                                                                                                )
</div>
4

1 回答 1

0

如果您想在模型为空时隐藏网格,那么您可以在其中放置一个 if 语句来检查模型是否为空......即。

if (model != null)
{
    // Make and populate grid here
}
于 2014-03-19T09:22:39.133 回答