locked
Validation are not disappearing , they are pre-loaded RRS feed

  • Question

  • User-588203678 posted

    Hi,

    I have controller,view and model. The validation are not firing in razor, they are pre-loaded. Where I had gone wrong... need help

    Controller:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using MvcSample.Models;
    using MvcSample.Repository;
    namespace MvcSample.Controllers
    {
        public class ShoppingController : Controller
        {
            //
            // GET: /Shopping/
            private ShoppingDBContext db = new ShoppingDBContext();
    
    
            public ActionResult PurchaseItems()
            {
                return View();
            }
    
            [HttpPost]
    
            public ActionResult SaveItems(ShoppingCartModels shopModels)
            {
                try
                {
                    if (ModelState.IsValid)
                    {
                       // db.Shop.Add(shopModels);
                       
    
                        db.SaveChanges();
                        return View(shopModels);
                        // return RedirectToAction("Index");
                    }
    
                    return View(shopModels);
                }
                catch (Exception Ex)
                {
                    throw Ex;
                }
            }
        }
    }
    
    
    
    Model:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity;
    
    namespace MvcSample.Models
    {
        public class ShoppingCartModels
        {
           
            [Display(Name = "Category")]
    
            public string lblItemCategory { get; set; }
          
            [Display(Name = "Product Name")]
            public string lblProductName { get; set; }
    
            
             [Display(Name = "Quantity")]
            public string lblQuantity { get; set; }
    
           
             [Display(Name = "Price")]
            public string lblPrice { get; set; }
    
             [Required(ErrorMessage = "Please enter Category")]
            public string strItemCategory { get; set; }
    
              [Required(ErrorMessage = "Please enter Product")]
            public string strProductName { get; set; }
             [Required(ErrorMessage = "Please enter Quantity")]
            public string strQuantity { get; set; }
              [Required(ErrorMessage = "Please enter Price")]
            public string strPrice { get; set; }
    
            [Key]
            public int strProductId { get; set; }
        }
        //public class ShoppingClass : DbContext
        //{
        //    public ShoppingClass()
        //        : base("DefaultConnection")
        //    {
        //    }
    
        //    public DbSet<ShoppingCartModels> shopping { get; set; }
        //}
    }
    
    Repository:
    IShoppingRepository
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using MvcSample.Models;
    namespace MvcSample.Repository
    {
        interface IShoppingRepository
        {
            void SaveShoppingItems(ShoppingCartModels shops);
            List<ShoppingCartModels> GetAllItems();
        }
    }
    Shopping Repository:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using MvcSample.Models;
    namespace MvcSample.Repository
    {
        public class ShoppingRepository : IShoppingRepository
        {
    
            ShoppingDBContext _db = new ShoppingDBContext();
    
            public void SaveShoppingItems(ShoppingCartModels shop)
            {
                _db.Shop.Add(shop);
                _db.SaveChanges();
    
            }
    
            public List<ShoppingCartModels> GetAllItems()
            {
                return _db.Shop.ToList();
            }
        }
    }
    
    
    ShoppingDBContext.cs
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    using MvcSample.Models;
    namespace MvcSample.Repository
    {
        public class ShoppingDBContext : DbContext
        {
            public ShoppingDBContext()
                : base("DefaultConnection")
            {
    
            }
            public DbSet<ShoppingCartModels> Shop { get; set; }
        }
    }
    
    View:
    @model MvcSample.Models.ShoppingCartModels
    
    @{
        ViewBag.Title = "PurchaseItems";
    }
    
    <h2>PurchaseItems</h2>
    
    @using (Html.BeginForm("SaveItems","Shopping")) {
        @Html.ValidationSummary(true)
    
        <fieldset>
            <legend>ShoppingCartModels</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.lblItemCategory)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.strItemCategory)
                @Html.ValidationMessageFor(model => model.strItemCategory)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.lblProductName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.strProductName)
                @Html.ValidationMessageFor(model => model.strProductName)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.lblQuantity)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.strQuantity)
                @Html.ValidationMessageFor(model => model.strQuantity)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.lblPrice)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.strPrice)
                @Html.ValidationMessageFor(model => model.strPrice)
            </div>
    
        
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }

    Thursday, October 27, 2016 8:37 AM

All replies

  • User-588203678 posted

    Hi,

    Also on click of submit why it says error cannot find SaveItems.cshtml in Shared folder  like that?

    Thursday, October 27, 2016 8:40 AM
  • User1771544211 posted


    Hi guhananth,

    @section Scripts {

      @Scripts.Render("~/bundles/jqueryval")

    }

    Please check if the jQuery.js is missing in your view page.

    @section Scripts {
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryval")
    }

    Also on click of submit why it says error cannot find SaveItems.cshtml in Shared folder  like that?

    This is because you use return View(shopModels); in your SaveItems action, so it will find the SaveItems.cshtml in Views/Shopping folder, if the application can't find the SaveItems.cshtml in Views/Shopping folder, it will search the Share folder to find SaveItems.cshtml.

    You can try use RedirectToAction to redirect user to index page after create a record successfully.

    db.SaveChanges();
    //return View(shopModels);
    return RedirectToAction("Index");

    Best Regards,

    Jean

    Friday, October 28, 2016 5:34 AM
  • User-588203678 posted

    Jean

    Still the validation message is not disappearing ,even after entering data into text box. Please guide me.

    Monday, November 7, 2016 9:42 AM
  • User1771544211 posted

    Hi guhananth,

    Please open the Web browser's developer tool(F12) and check if there is any error message in the console.

    And view the source of the page to check if the js files(jquery.js, jquery.validate.js, jquery.validation.unovrtusive.js) are loaded correctly.

    The following link shows how to use F12 to check the errors, please take it as reference

    https://msdn.microsoft.com/en-us/library/gg589530(v=vs.85).aspx

    Best Regards,

    Jean

    Tuesday, November 8, 2016 2:14 AM
  • User1360833881 posted

    Dear Jean Sun,

    I also had the same problem (message not disappearing even after entering values in required Text Box), but your solution did wonders, after so many attempts and googling, I just added @Scripts.Render("~/bundles/jqueryval") in Layout page, and my issue got resolved. Thank you so much for contributing to the developers community.

    Thursday, July 9, 2020 9:30 PM