I am trying to send a List<Theatres> from a view to a controller using AJAX
each theates has the meanings theates (string), TheatresNumber (int), HomeCinemaID (int), RowAmount (int).
here is the model code:
public class MovieTheaters { [Key] public int MovieTheatersID { get; set; } public int HomeCinemaID { get; set; } public string TheatersName { get; set; } public int NumberHall { get; set; } public int RowAmount { get; set; }
the user enters the number of theaters he needs, then the cycle gives the opportunity to create them.
view code:
@model CimenaCityProject.Models.MovieTheaters @{ ViewBag.Title = "Create"; } <h2>Create New Theatres</h2> @Html.AntiForgeryToken() @{ int? maxNumberOfTheatres = ViewBag.number; if (!maxNumberOfTheatres.HasValue) { maxNumberOfTheatres = 1; } } @using (Html.BeginForm("Create", "Theatres", FormMethod.Post, new { name = "TheatresForm", id = "TheatresForm" })) { @Html.ValidationSummary(true) <table> <tbody> @for (int i = 0; i < maxNumberOfTheatres.Value; i++) { <tr> <td id="NewTheaters"> @ViewBag.ErrorMassage <div style="position:relative; top: 0px; left: 205px; width: 278px;"> @string.Format("Theares Number {0}", i + 1) </div> <div> <br /> <div class="form-group"> @Html.LabelFor(model => model.HomeCinemaID, "HomeCinemaID", new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("HomeCinemaID") @Html.ValidationMessageFor(model => model.HomeCinemaID) </div> <br /> </div> <div class="form-group"> @Html.LabelFor(model => model.TheatersName, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.TheatersName) @Html.ValidationMessageFor(model => model.TheatersName) </div> <br /> </div> <div class="form-group"> @Html.LabelFor(model => model.NumberHall, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.NumberHall) @Html.ValidationMessageFor(model => model.NumberHall) </div> <br /> </div> <div class="form-group"> @Html.LabelFor(model => model.RowAmount, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.RowAmount) @Html.ValidationMessageFor(model => model.RowAmount) </div> </div> </div> </td> </tr> } </tbody> </table> } <div class="form-group"> <div class="col-lg-push-9"> <input type="submit" name="Create" value="Create" id="Create" /> </div> </div> <div id="divLoading" style="display: none; align-items: center"> <img src="~/Image/Elements/ajax-loader.gif" /> </div> <div id="divResult"></div> <div> @Html.ActionLink("Back to List", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Ajax Code:
<script type="text/javascript"> $(document).on('#TheatresForm' ,function () { var NewTheaters = []; $('table tbody tr td').each(function () { NewTheaters.push({ HomeCinemaID: $('#HomeCinemaID').val(), TheatersName: $('#TheatersName').val(), NumberHall: $('#NumberHall').val(), RowAmount: $('#RowAmount').val() }); }); $('#divLoading').show() $.ajax({ url: '@Url.Action()', type: 'POST', traditional : true, data: JSON.stringify(NewTheaters), contentType: 'application/json; charset=utf-8', success: function (result) { success(result) }, error: function (result) { alert(result.responseText + "Error") $('#divLoading').hide() } }); function success(result) { alert("success") $('#divResult').html(result) $('#divLoading').hide() } });
Controller code
// GET: /Theatres/Create public ActionResult Create(int? id, int? number) { if (id == null) { ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas, "HomeCinemaID", "CinemaName"); number = 1; ViewBag.number = number; } else { ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas.Where(x => x.HomeCinemaID == id).ToArray(), "HomeCinemaID", "CinemaName"); ViewBag.number = number; } ViewBag.ErrorMassage = ""; return View(); } // POST: /Theatres/Create [HttpPost] public ActionResult Create( List<MovieTheaters> NewTheaters) { foreach (var movietheaters in NewTheaters) { if (db.Theaters.Where(x => x.TheatersName == movietheaters.TheatersName && x.HomeCinemaID == movietheaters.HomeCinemaID).FirstOrDefault() == movietheaters) { ViewBag.ErrorMassage = "You cant add this Theatres again. Choose another name."; ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas.Where(x => x.HomeCinemaID == movietheaters.HomeCinemaID).ToArray(), "HomeCinemaID", "CinemaName", movietheaters.HomeCinemaID); return View(movietheaters); } else { if (ModelState.IsValid) { db.Theaters.Add(movietheaters); db.SaveChanges(); return RedirectToAction("Create", "Rows", new { id = movietheaters.MovieTheatersID, rwcpcty = movietheaters.RowAmount, last = 1 }); } } ViewBag.ErrorMassage = "Try again later"; ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas, "HomeCinemaID", "CinemaName", movietheaters.HomeCinemaID); return View(movietheaters); } ViewBag.ErrorMassage = "Try again later"; ViewBag.HomeCinemaID = new SelectList(db.HomeCinemas, "HomeCinemaID", "CinemaName"); return View(); }
evry time it throws me zero .. what do i need to do?