Click here to Skip to main content
Sign Up to vote bad
good
See more: ASP.NETAjax
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 - 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
Your Filters
Interested
Ignored
     
0 Christian Graus 578
1 Ron Beyer 326
2 Tadit Dash 253
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,041
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,034


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