Click here to Skip to main content
12,398,816 members (55,739 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET Ajax
I need to edit the contents of the given table using AJAX but when I Edit the name a new row is created please Help me to solve the problem my code is attached below...

ViewPAge.cstml

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

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 18-Nov-12 23:21pm
Comments
Christian Graus 19-Nov-12 17:05pm
   
What happens when you step through the debugger ? Why does your AJAX code redirect after the call, what is the point of that ?
Member 9578424 19-Nov-12 22:28pm
   
when its debugged I can edit the genre and Release date but when I edit the Title a new row is created with the edited title..

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100