2012-08-02 45 views
0

1below我發佈了我的總編碼視圖。這裏驗證不是在文本框中觸發。我不知道如何解決這個問題。該視圖正在執行。如果我按下搜索文本框而不輸入文本框中的文本,它不驗證。另外告訴我,我必須使用TextBox或TextBoxFor。我對mvc3更加新鮮。請告訴我解決方案。驗證不在RAZOR中的文本框上觸發?

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

     } 
+0

你爲你的文本框設置哪些驗證規則?你確定你做到了嗎? – 2012-08-02 06:44:36

+0

你的模型是什麼樣的?您是否使用[StringLength(50,ErrorMessage =「最大長度爲50個字符」)]或[Required(ErrorMessage =「此字段是必需的」)]屬性來填充字段 – user1304444 2012-08-02 06:46:09

+0

@ FSou1:[必需(ErrorMessage =「BrandNamest必填「)] public string BrandName {get;組; }我已經在 – Sham 2012-08-02 06:51:10

回答

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" /> 
} 
+0

告訴把webgrid放在局部視圖中,好吧,我做到了,現在如何將局部視圖調用到當前視圖頁面..是我理解的正確的.. – Sham 2012-08-02 07:34:01

+0

我不完全明白你在問什麼......你想知道如何渲染局部視圖嗎?像這樣:@ Html.RenderPartial(「PartialViewName」,model)...看完這個之後,它可能只會將搜索框放在局部視圖中,而不是網格...(否則你從哪裏得到模型?) – user1304444 2012-08-02 07:50:26

+0

現在也驗證不工作... – Sham 2012-08-02 08:36:57