使用Ajax将多个实例传递给控制器

时间:2021-04-17 11:28:23

I try to send a List<Theatres> from view to controller by using AJAX

我尝试使用AJAX将List 从视图发送到控制器

each theates have a TheatresName (string), TheatresNumber (int), HomeCinemaID (int), RowAmount (int).

每个主题都有一个TheatresName(字符串),TheatresNumber(int),HomeCinemaID(int),RowAmount(int)。

here 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; }

        //FK .
        public virtual HomeCinema HomeCinema { get; set; }
        public virtual ICollection<Rows> Rows { get; set; }

    }

the user enter how many theatres he need then for loop gave the option 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:

Ajax代码:

<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 throw me a null .. what i have to do?

evry time它给我一个空..我要做什么?

2 个解决方案

#1


1  

According to me the best you can implement is this, This will surely work for you:

据我所知,你可以实现的最好的是,这肯定会对你有用:

Change your html to this : Firstly change your above model to this model

将您的HTML更改为:首先将您的上述模型更改为此模型

@model List<CimenaCityProject.Models.MovieTheaters>

@{
   var selectlist = (SelectList)ViewBag.HomeCinemaID;
 }

Then your form should be like this, Changes i made are indexing also given to all your editors and dropdownlist which will give you a different name to every field. Remember to bind your list to your dropdown as i have written their "Your List":

然后您的表单应该是这样的,我所做的更改也会给所有编辑器和下拉列表编制索引,这将为每个字段提供不同的名称。记得将我的列表绑定到您的下拉列表,因为我写了他们的“您的列表”:

@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[i].HomeCinemaID, "HomeCinemaID", new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.DropDownListFor(model => model[i].HomeCinemaID,selectlist)
                                    @Html.ValidationMessageFor(model => model[i].HomeCinemaID)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].TheatersName, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].TheatersName)
                                    @Html.ValidationMessageFor(model => model[i].TheatersName)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].NumberHall, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].NumberHall)
                                    @Html.ValidationMessageFor(model => model[i].NumberHall)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].RowAmount, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].RowAmount)
                                    @Html.ValidationMessageFor(model => model[i].RowAmount)
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    }
    <div class="form-group">
            <div class="col-lg-push-9">
            <input type="button" name="Create" value="Create" id="Create" onclick="SaveTheatre()" />
        </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")
         }

Now your script code should be like this, Just send your whole form by serializing it in jquery $('form:first').serializeArray();:

现在你的脚本代码应该是这样的,只需通过在jquery $('form:first')中序列化它来发送整个表单.serializeArray();:

function SaveTheatre() {
    $.ajax({
        url: '@Url.Action("Create","Home")',
        type: 'POST',
        data: $('form:first').serializeArray(),
        success: function(result) {
            success(result)
        },
        error: function(result) {
            alert(result.responseText + "Error") $('#divLoading').hide()
        }
    });
}

function success(result) {
    alert("success") $('#divResult').html(result) $('#divLoading').hide()
}

All done , You will get your values now in controller :)

一切都完成了,你现在将在控制器中获得你的价值:)

#2


2  

ok. thank you very much @Rohit Arora and @Hemant Bhagat here the full answer working very good.

好。非常感谢@Rohit Arora和@Hemant Bhagat,这里的答案非常好。

Code Controller :

代码控制器:

        // 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();
    }

View Code:

查看代码:

    @model List<CimenaCityProject.Models.MovieTheaters>

@{
    ViewBag.Title = "Create";
    var selectlist = (SelectList)ViewBag.HomeCinemaID;
}

<h2>Create New Theatres</h2>

@Html.AntiForgeryToken()

@{
    int? maxNumberOfTheatres = ViewBag.number;
    if (!maxNumberOfTheatres.HasValue)
    {
        maxNumberOfTheatres = 1;
    }
}

@using (Html.BeginForm("Create", "Theatres", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <table>
        <tbody>
            @for (int i = 0; i < maxNumberOfTheatres.Value; i++)
        {
            <tr>
                <td id="NewTheaters" style="width:700px">
                    @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[i].HomeCinemaID, "HomeCinemaID", new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.DropDownListFor(model => model[i].HomeCinemaID, selectlist)
                                @Html.ValidationMessageFor(model => model[i].HomeCinemaID)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].TheatersName, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].TheatersName)
                                @Html.ValidationMessageFor(model => model[i].TheatersName)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].NumberHall, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].NumberHall)
                                @Html.ValidationMessageFor(model => model[i].NumberHall)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].RowAmount, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].RowAmount)
                                @Html.ValidationMessageFor(model => model[i].RowAmount)
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>
}
<div class="form-group">
    <div class="col-lg-push-9">
        <input type="button" name="Create" value="Create" id="Create"     onclick="SaveTheatre()" />
    </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 :

Ajax代码:

    <script src="~/Scripts/jquery-1.10.2.js"></script>
 <script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.sigmacape.unobtrusive-1.1.2.js"></script>

    <script type="text/javascript">
 function SaveTheatre() {
    $.ajax({
    url: '@Url.Action()',
    type: 'POST',
    traditional : true,
    data: $('form:first').serializeArray(),
//    contentType: 'application/json; charset=utf-8', ---> this will give you Exeption Invalid JSON Primitive!! 
    success: function (result) {
        success(result)
    },
    error: function (result) {
        alert(result.responseText + "Error")
        $('#divLoading').hide()
    }
    });
}

    function success(result) {
    alert("success")
    $('#divResult').html(result)
    $('#divLoading').hide()
    }
    </script>

#1


1  

According to me the best you can implement is this, This will surely work for you:

据我所知,你可以实现的最好的是,这肯定会对你有用:

Change your html to this : Firstly change your above model to this model

将您的HTML更改为:首先将您的上述模型更改为此模型

@model List<CimenaCityProject.Models.MovieTheaters>

@{
   var selectlist = (SelectList)ViewBag.HomeCinemaID;
 }

Then your form should be like this, Changes i made are indexing also given to all your editors and dropdownlist which will give you a different name to every field. Remember to bind your list to your dropdown as i have written their "Your List":

然后您的表单应该是这样的,我所做的更改也会给所有编辑器和下拉列表编制索引,这将为每个字段提供不同的名称。记得将我的列表绑定到您的下拉列表,因为我写了他们的“您的列表”:

@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[i].HomeCinemaID, "HomeCinemaID", new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.DropDownListFor(model => model[i].HomeCinemaID,selectlist)
                                    @Html.ValidationMessageFor(model => model[i].HomeCinemaID)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].TheatersName, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].TheatersName)
                                    @Html.ValidationMessageFor(model => model[i].TheatersName)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].NumberHall, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].NumberHall)
                                    @Html.ValidationMessageFor(model => model[i].NumberHall)
                                </div>
                                <br />
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model[i].RowAmount, new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model[i].RowAmount)
                                    @Html.ValidationMessageFor(model => model[i].RowAmount)
                                </div>
                            </div>
                        </div>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    }
    <div class="form-group">
            <div class="col-lg-push-9">
            <input type="button" name="Create" value="Create" id="Create" onclick="SaveTheatre()" />
        </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")
         }

Now your script code should be like this, Just send your whole form by serializing it in jquery $('form:first').serializeArray();:

现在你的脚本代码应该是这样的,只需通过在jquery $('form:first')中序列化它来发送整个表单.serializeArray();:

function SaveTheatre() {
    $.ajax({
        url: '@Url.Action("Create","Home")',
        type: 'POST',
        data: $('form:first').serializeArray(),
        success: function(result) {
            success(result)
        },
        error: function(result) {
            alert(result.responseText + "Error") $('#divLoading').hide()
        }
    });
}

function success(result) {
    alert("success") $('#divResult').html(result) $('#divLoading').hide()
}

All done , You will get your values now in controller :)

一切都完成了,你现在将在控制器中获得你的价值:)

#2


2  

ok. thank you very much @Rohit Arora and @Hemant Bhagat here the full answer working very good.

好。非常感谢@Rohit Arora和@Hemant Bhagat,这里的答案非常好。

Code Controller :

代码控制器:

        // 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();
    }

View Code:

查看代码:

    @model List<CimenaCityProject.Models.MovieTheaters>

@{
    ViewBag.Title = "Create";
    var selectlist = (SelectList)ViewBag.HomeCinemaID;
}

<h2>Create New Theatres</h2>

@Html.AntiForgeryToken()

@{
    int? maxNumberOfTheatres = ViewBag.number;
    if (!maxNumberOfTheatres.HasValue)
    {
        maxNumberOfTheatres = 1;
    }
}

@using (Html.BeginForm("Create", "Theatres", FormMethod.Post))
{
    @Html.ValidationSummary(true)
    <table>
        <tbody>
            @for (int i = 0; i < maxNumberOfTheatres.Value; i++)
        {
            <tr>
                <td id="NewTheaters" style="width:700px">
                    @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[i].HomeCinemaID, "HomeCinemaID", new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.DropDownListFor(model => model[i].HomeCinemaID, selectlist)
                                @Html.ValidationMessageFor(model => model[i].HomeCinemaID)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].TheatersName, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].TheatersName)
                                @Html.ValidationMessageFor(model => model[i].TheatersName)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].NumberHall, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].NumberHall)
                                @Html.ValidationMessageFor(model => model[i].NumberHall)
                            </div>
                            <br />
                        </div>

                        <div class="form-group">
                            @Html.LabelFor(model => model[i].RowAmount, new { @class = "control-label col-md-2" })
                            <div class="col-md-10">
                                @Html.EditorFor(model => model[i].RowAmount)
                                @Html.ValidationMessageFor(model => model[i].RowAmount)
                            </div>
                        </div>
                    </div>
                </td>
            </tr>
        }
    </tbody>
</table>
}
<div class="form-group">
    <div class="col-lg-push-9">
        <input type="button" name="Create" value="Create" id="Create"     onclick="SaveTheatre()" />
    </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 :

Ajax代码:

    <script src="~/Scripts/jquery-1.10.2.js"></script>
 <script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/jquery.sigmacape.unobtrusive-1.1.2.js"></script>

    <script type="text/javascript">
 function SaveTheatre() {
    $.ajax({
    url: '@Url.Action()',
    type: 'POST',
    traditional : true,
    data: $('form:first').serializeArray(),
//    contentType: 'application/json; charset=utf-8', ---> this will give you Exeption Invalid JSON Primitive!! 
    success: function (result) {
        success(result)
    },
    error: function (result) {
        alert(result.responseText + "Error")
        $('#divLoading').hide()
    }
    });
}

    function success(result) {
    alert("success")
    $('#divResult').html(result)
    $('#divLoading').hide()
    }
    </script>