0

1below I have posted my total coding of view. Here validation is not firing on the textbox. I dont know how to sort out this problem. This view is executing. if I press the search textbox without entering the text in textbox, its not validating.Also tell me wheather I have to use TextBox or TextBoxFor. I am fresher and new to mvc3. Please tell me the solution.

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

      }
Sham
  • 1,191
  • 2
  • 13
  • 25
  • What validation rule did you set for your textboxes? And are you sure you did it? –  Aug 02 '12 at 06:44
  • What does your model look like? Did you decorate the fields with attributes like [StringLength(50, ErrorMessage="Max length is 50 characters")] or [Required(ErrorMessage="This field is required")] – user1304444 Aug 02 '12 at 06:46
  • @FSou1: [Required(ErrorMessage = "BrandNamest is Required")] public string BrandName { get; set; } I have used the mentioned required validation in model – Sham Aug 02 '12 at 06:51
  • @user1304444: yeah used it... – Sham Aug 02 '12 at 06:51
  • In above wheather i have to use the textbox or textboxfor.. – Sham Aug 02 '12 at 06:52
  • I usually use "EditorFor" but "TextBoxFor" should work fine. That is to say, use TextBoxFor, not TextBox. – user1304444 Aug 02 '12 at 06:54
  • @Yasser @Html.LabelFor(o => o.BrandName[i]) @Html.TextBoxFor(o => o.BrandName) @Html.ValidationMessageFor(o => o.BrandName[i]) I have used the above and removed the webgrid clientside message is displaying – Sham Aug 02 '12 at 06:56
  • @Yasser: Since I am passing the value as Ienumerable to the view. If i used the texboxfor after the "o" in intelligence the variable name is not displayin. " @Html.TextBoxFor(o => o. " my explaination is not good sorry for that.. – Sham Aug 02 '12 at 07:04
  • 1
    I notice you have TextBoxFor(o => o.BrandName) and LabelFor(o => o.BrandName[i]) Did you leave the enumerator out on purpose? If you want every item in the model, you will need a loop. – user1304444 Aug 02 '12 at 07:05
  • @user1304444 : yeah I need loop because based on the brandname am retriving the values which i Mentioned in grid.. – Sham Aug 02 '12 at 07:11

1 Answers1

0

Edit: added a more complete example.

I'm not sure from the comments above if it's working to your satisfaction or not, but I think the root of your issue is that you are trying to use a model of IEnumerable where you don't need one. I understand that you need it for your grid, but what if you either put the grid or the search box in a PartialView? The PartialView could have its own model, and then you wouldn't need to fit your search box into a model that doesn't really fit.

The way I understand it, you're not actually passing data into the search box. You are only using the model at the search box so you get field validation. Let me know if that is incorrect.

Edit your view to look like this:

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

  }

Create a Partial View (_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" />
}
user1304444
  • 1,713
  • 3
  • 21
  • 38
  • u telling to put the webgrid in partial view. Ok i did it. now how to call the partial view to current view page.. IS what i understood is correct.. – Sham Aug 02 '12 at 07:34
  • I don't completely understand what you're asking... Do you want to know how to render the partial view? something like this: @Html.RenderPartial("PartialViewName", model)...After looking at this, it may only work to put the search box in a partial view, not the web grid...(else where do you get the model from?) – user1304444 Aug 02 '12 at 07:50
  • @model IEnumerable< ShoppingCart.Models.ShoppingClass>..If I remove this Validation is firing. – Sham Aug 02 '12 at 10:17
  • I am passing List from Model to view , so I specified in view like this IEnumerable< ShoppingCart.Models.ShoppingClass> But, In this situation Client side validation is not firing. – Sham Aug 02 '12 at 10:24
  • @model IEnumerable< ShoppingCart.Models.ShoppingClass>, here have declared the view as Ienumerable, so here @{Html.RenderPartial("_Search", Model.First());} after model i cannot access my funtion name – Sham Aug 03 '12 at 05:24
  • I don't understand what you mean by `after model I cannot access my function name` Can you clarify that? – user1304444 Aug 03 '12 at 06:15
  • @{Html.RenderPartial("_Search", Model.Searching(brandname))}, if given like this means it showing error that brandname does not exists in current context – Sham Aug 03 '12 at 06:19
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14835/discussion-between-raj2sekar1-and-user1304444) – Sham Aug 03 '12 at 07:10