Click here to Skip to main content
15,909,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
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 ?
Alen Joy 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)



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