0

1below 我已经发布了我的视图总编码。这里验证不会在文本框上触发。我不知道如何解决这个问题。该视图正在执行。如果我按下搜索文本框而不在文本框中输入文本,它不会验证。还告诉我是否必须使用 TextBox 或 TextBoxFor。我对 mvc3 比较陌生。请告诉我解决方案。

    @model  IEnumerable< ShoppingCart.Models.ShoppingClass>
        @{
            ViewBag.Title = "Display";

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

@Html.ValidationSummary(true)
 @using (Html.BeginForm("Display","Home", FormMethod.Post, new { id = "loginForm" }))


        {

            //for (int i = 0; i < 1; i++)
            //{
            <table><tr>o<td> @Html.Label("BrandName")</td>
            <td>@Html.TextBox("BrandName") <div> @Html.ValidationMessage("BrandName")</div></td>
         <td></td></tr></table>



    @*    <table><tr><td>

         @Html.LabelFor(o=>o[i].BrandName)</td>
        <td>@Html.EditorFor(o => o[i].BrandName) <div>@Html.ValidationMessageFor(o => o[i].BrandName)</div></td>
        <td></td></tr></table>*@



          //  }
               <input type="submit" value="Search" name="Search" />
        }



  @{
    var grid = new WebGrid(source: Model, defaultSort: "Drug_Code", rowsPerPage: 20);
 <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

编辑:添加了一个更完整的示例。

从上面的评论中我不确定它是否能让你满意,但我认为你的问题的根源是你试图在不需要的地方使用 IEnumerable 模型。我知道您的网格需要它,但是如果您将网格或搜索框放在 PartialView 中怎么办?PartialView 可以有自己的模型,然后您不需要将搜索框放入不适合的模型中。

据我了解,您实际上并没有将数据传递到搜索框中。您仅在搜索框中使用模型,因此您可以获得字段验证。让我知道这是否不正确。

编辑您的视图,使其如下所示:

@model  IEnumerable< ShoppingCart.Models.ShoppingClass>
    @{
        ViewBag.Title = "Display";
    }
   <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>

@Html.ValidationSummary(true)

@{Html.RenderPartial("_Search", Model.First());} //Need to make sure Model.First() actually exists, or you'll get error when it doesn't
//If you change your partial view to have a view model with just brand name, call it like this:
//@Html.RenderPartial("_Search", new _SearchViewModel())
//If you're only using the view model for required field validation on the return trip, then you don't actually need to pass data down to it.

@{
  var grid = new WebGrid(source: Model, defaultSort: "Drug_Code", rowsPerPage: 20);
 <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>

  }

创建局部视图(_Search):

@model ShoppingCart.Models.ShoppingClass
//You could also create a different View Model that just has brand name, and pass the data down another way
//See examples in the code for the view

@Html.ValidationSummary(true)
@using (Html.BeginForm("Display","Home", FormMethod.Post, new { id = "loginForm" }))
{
    <table><tr><td>

     @Html.LabelFor(o=>o.BrandName)</td>
    <td>@Html.EditorFor(o => o.BrandName) <div>@Html.ValidationMessageFor(o => o.BrandName)</div></td>
    <td></td></tr></table>

    <input type="submit" value="Search" name="Search" />
}
于 2012-08-02T07:22:47.933 回答