Click here to Skip to main content
Rate this: bad
good
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>
    <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 18-Nov-12 23:21pm
Comments
Christian Graus at 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 at 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
0 CPallini 365
1 Sergey Alexandrovich Kryukov 252
2 OriginalGriff 165
3 George Jonsson 159
4 Richard MacCutchan 110
0 OriginalGriff 6,344
1 Sergey Alexandrovich Kryukov 5,860
2 CPallini 5,125
3 George Jonsson 3,559
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 19 Nov 2012
Copyright © CodeProject, 1999-2014
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