@model WebApplication14.Domain.Concrete.ViewCurrentDisciples @{ ViewBag.Title = "Test"; } <html> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/ajax") <body> <div id="Results_Section">@{Html.RenderAction("FirstSpisok", "Test");}</div> <div class="col-md-6 .col-md-offset-3"> <button class="btn btn-primary" onclick="PostData()">Отправить</button> </div> <script type="text/javascript"> $(document).ready(function () { if($('div[class = "col-md-4"]').is(":hidden")) { $('div[class = "col-md-4"]').slideDown(2000); } }); function PostData() { var params = new Array(); var napr = new Array(); var testdata = { 'Params': params, 'Napr': napr } $('input[class = "checking"]:checked').each(function () { params.push($(this).val()); napr.push($(this).attr('id')); }) $.ajax({ type: "POST", url: '/Test/FirstSpisok', data: JSON.stringify(testdata), contentType: "application/json; charset=utf-8", dataType: "json", success: successFunc, async : true } ); //$('div[class = "col-md-4"]').slideUp(2000); function successFunc(data) { console.log("sdfsdf" + data); $("#Results_Section").append(data); } } function errorFunc(errorData) { alert('Ошибка' + errorData.responseText); } let a = function(spec, value) { } </script> </body> </html> 

Controller

  using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using WebApplication14.Models.DataBaseModel; using WebApplication14.Domain.Concrete; using WebApplication14.Domain.Abstract; namespace WebApplication14.Controllers { public class TestController : Controller { public PartialViewResult FirstSpisok() { return PartialView(new ViewCurrentDisciples { sp = (SpecConstructor)Session["Cart"] }); } [HttpPost] public JsonResult FirstSpisok(ModelOfOutPut mf) { return Json(new ViewCurrentDisciples { sp = (SpecConstructor)Session["Cart"] }); } } 

}

  • Well, what exactly is the question? The error here is probably only in the line $("Results_Section").append(); because invalid selector (must be either #Result_section or .Result_section) and does not add anything as a parameter to the append function (assuming that there will be data from the responce) - alexoander
  • The question is that I want to return a partial view to update the one that already exists, after .apen () does not cost anything since this option does not work - Electronik
  • your data is sent as a string. But you need a key value. - Artem Gorlachev
  • @ArtemGorlachev, not necessarily api.jquery.com/jquery.ajax - br3t

1 answer 1

Why do you return JsonResult in the second case? Json is usually used if you want to return some kind of data set, for example, an array or an object.

But in your case, as I understand it, you want to replace old content with new one, then return PartialViewResult , and then replace old html with new one using the .html () function.

Representation:

 @model WebApplication14.Domain.Concrete.ViewCurrentDisciples @{ ViewBag.Title = "Test"; } <html> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/ajax") <body> <div id="Results_Section">@{Html.RenderAction("FirstSpisok", "Test");}</div> <div class="col-md-6 .col-md-offset-3"> <button class="btn btn-primary" onclick="PostData()">Отправить</button> </div> <script type="text/javascript"> $(document).ready(function () { if ($('div.col-md-4').is(":hidden")) { $('div.col-md-4').slideDown(2000); } }); function PostData() { var params = new Array(); var napr = new Array(); var testdata = { 'Params': params, 'Napr': napr } $('input.checking:checked').each(function () { params.push($(this).val()); napr.push($(this).attr('id')); }) $.post('/Test/FirstSpisok', testdata).done(successFunc).fail(errorFunc); } function successFunc(data) { $("#Results_Section").html(data); } function errorFunc(errorData) { alert('Ошибка' + errorData.responseText); } </script> </body> </html> 

Controller:

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using WebApplication14.Models.DataBaseModel; using WebApplication14.Domain.Concrete; using WebApplication14.Domain.Abstract; namespace WebApplication14.Controllers { public class TestController : Controller { public PartialViewResult FirstSpisok() { return PartialView(new ViewCurrentDisciples {sp = (SpecConstructor) Session["Cart"]}); } [HttpPost] public PartialViewResult FirstSpisok(ModelOfOutPut mf) { //какой-то код return PartialView(new ViewCurrentDisciples {sp = (SpecConstructor) Session["Cart"]}); } } }