Hello. Here I solved the problem with connecting the "js" code to a partial view but did not stop there and found the following problem for myself. If you connect "js" on the main page ("_ Layout.cshtml") then the code is available when you first load the page, but after updating (loading using ajax) it stops working, if you connect in a partial view, the situation is reversed, and connect the script twice somehow wrong. So the question itself: What do you do in this case?
Upd:
javascript
$('.select2').each(function () { var url = this.dataset.url; $(this).select2({ delay: 100, allowClear: true, minimumInputLength: 1, // minimumInputLength for sending ajax request to server width: 'resolve', // to adjust proper width of select2 wrapped elements ajax: { url: url, type: "GET", dataType: 'json', data: function (term) { return { searchStr: term, }; }, results: function (data) { return { results: data.list }; // data.CountryList returning json data from Controller } } }) })
partial view
<div id="tabItems"> @using (Ajax.BeginForm("AddOrderItem", "Order", null, new AjaxOptions { UpdateTargetId = "tabItems", HttpMethod = "Post" }, new { id = "AddItem" })) { @Html.AntiForgeryToken() <div class="nav"> <div class="navbar-form navbar-left"> @Html.ValidationSummary(true, "", new { @class = "text-danger" }) @Html.HiddenFor(model => model.OrderId, new { id="OrderId" }) <div class="form-group"> @Html.EditorFor(model => model.ProductId, new { htmlAttributes = new { id = "ProductId", @class = "form-control select2", data_url = @Url.Action("GetProductList", "Order"), data_placeholder = "Select a product" } }) @Html.ValidationMessageFor(model => model.ProductId, "", new { @class = "text-danger" }) </div> <div class="form-group"> @Html.EditorFor(model => model.ProductNodeId, new { htmlAttributes = new { id = "ProductNodeId", @class = "form-control select2", data_url = @Url.Action("GetProductNodeList", "Order") } }) @Html.ValidationMessageFor(model => model.ProductNodeId, "", new { @class = "text-danger" }) </div> <div class="form-group"> @Html.EditorFor(model => model.ToolId, new { htmlAttributes = new { id="ToolId", @class = "form-control select2", data_url = @Url.Action("GetToolList", "Order") } }) @Html.ValidationMessageFor(model => model.ToolId, "", new { @class = "text-danger" }) </div> <div class="form-group"> @Html.EditorFor(model => model.AmountRequired, new { htmlAttributes = new { id="AmountRequired", @class = "form-control" } }) @Html.ValidationMessageFor(model => model.AmountRequired, "", new { @class = "text-danger" }) </div> <div class="form-group"> <a href="#" id="submit" class="btn btn-primary"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></a> </div> </div> </div> } </div>
controller code
public ActionResult AddOrderItem(AddOItemModel model) { //здесь какой то код return RedirectToAction("GetOrderItems", new {orderId = model.OrderId}) } public ActionResult GetOrderItems(int orderId) { var model=//получение модели для представления return PartialView("Items",model) }
UPD2: the script took this form
$('body').on('click', '.select2', function () { $(this).each(function () { var url = this.dataset.url; $(this).select2({ delay: 100, allowclear: true, minimuminputlength: 1, // minimuminputlength for sending ajax request to server width: 'resolve', // to adjust proper width of select2 wrapped elements ajax: { url: url, type: "get", datatype: 'json', data: function (term) { return { searchstr: term, }; }, results: function (data) { return { results: data.list }; // data.countrylist returning json data from controller } } }) }) })