Click here to Skip to main content
14,738,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to validate the text boxes using AJAX. I'm new to AJAX so please teach me.. My Code is given below

ViewPAge.cshtml
<script type="text/javascript" language="javascript">
 
      $(document).ready(function () {
          $("#update").click(function () {
              var flag = 1;
              var title = $("Title").val();
              var genre = $("Genre").val();
              var rel_date = $("Release_Date").val();
              $.ajax({
                  type: "POST",
                  url: "../Home/Editdata",
                  data: $('#movie_info').serialize(),
                  success: function (data) {
                      if (flag == 1) {
                          window.location = "Index";
                      }
                      else {
                          alert("Error Occured");
 
                      }
                  }
              });
          });
      });
    </script>
    <form action="Edit" id= "movie_info">
   <fieldset>
            <legend>Edit Title</legend>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Title)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Title)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Genre)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Genre)
            </div>
    
            <div class="editor-label">
                @Html.LabelFor(model => model.Release_Date)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Release_Date)
            </div>
         </fieldset>
    </form> 
    <div>
     <input type="button" value="update" id="update" />           
    </div>


HomeController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using movie_index.Models;
 
namespace movie_index.Controllers
{
    public class HomeController : Controller
    {
        private static Movie_Data Movie_title = new Movie_Data();
 
        public ActionResult Index()
        {
            return View(Movie_title.MovieData);
        }
 
        public ViewResult Create()
        {
            return View(new Movie());
        }
        public void CreateData(Movie item)
        {
            Movie_title.Create(item);
 
        }
        public ViewResult Edit(string mov)
        {
            return View(Movie_title.Getitems(mov));
        }
        public void Editdata(Movie item)
        {
            Movie_title.Edit(item.Title);
            Movie_title.Create(item);
 
        }
        public ActionResult Delete(string mov)
        {
            Movie_title.Edit(mov);
            return RedirectToAction("Index");
        }
 
    }
}


Model.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
 
namespace movie_index.Models
{
    public class Movie
    {
        public string Title { get; set; }
        public string Genre { get; set; }
        public string Release_Date { get; set; }
    }
 
    public class Movie_Data
    {
        public List<Movie> MovieData = new List<Movie>();
        public Movie_Data()
        {
                MovieData.Add(new Movie {  Title = "Inception", Genre = "Thriller", Release_Date ="01/12/2012" });
                MovieData.Add(new Movie {  Title = "Dark knight", Genre = "Thriller", Release_Date = "01/12/2012" });
                MovieData.Add(new Movie {  Title = "Water For Elephants", Genre = "Drama", Release_Date = "01/12/2012" });
                MovieData.Add(new Movie {  Title = "The Help", Genre = "Drama", Release_Date = "01/12/2012" });
        }
        public void Update(Movie Mov_Update)
        {
            foreach (Movie item in MovieData)
            {
                if (item.Title == Mov_Update.Title)
                {
                    MovieData.Remove(item);
                    MovieData.Add(Mov_Update);
                }
 
            }
        }
        public void Create(Movie new_tit)
        {
            var newlst = MovieData.ToList();
            bool found = false;
            foreach (Movie item in MovieData)
            {
                if (new_tit.Title== item.Title)
                {
                    found = true;
                }
            }
            if (!found)
                MovieData.Add(new_tit);
 
        }
        public void Edit(string mov_lst)
        {
 
            foreach (Movie item in MovieData)
            {
                if (item.Title == mov_lst)
                {
                    MovieData.Remove(item);
                    break;
                }
            }
 
        }
        public Movie Getitems(string mov)
        {
            Movie tit_list = null;
            foreach (Movie item in MovieData)
            {
                if (item.Title == mov)
                {
                    tit_list = item;
                }
            }
            return tit_list;
        }
 
    }
}
Posted

1 solution

Your question is not specific. Where do you need the validation? If you are looking for client side validation using javascript/jQuery following are some links that will help to build your concept:
ASP.NET MVC3 Razor With jQuery For Beginners
Validation In Razor
ASP.NET MVC 3 Unobtrusive Javascript Validation With Custom Validators

You can also do some validations using Data Annotation Or Fluent API. Julie Lerman describes nicely in her articles how to work with those. You will find many more articles on google.

Hope it helps.
   
Comments
Alen Joy 21-Nov-12 1:13am
   
Problem solved thank you :)
Monjurul Habib 21-Nov-12 2:10am
   
thank you too :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900