Help me figure out the third day!

I have on my page a pop-up list with sizes of clothes. Depending on the size chosen, the price of the product on the page should change. To work with the database, I use entity framrework. Using jquery, I created an event handler for changing the value in the list. What you need to enter in function (data) to return the html markup with the price per page.

Here is the change () event handler itself.

<script type="text/javascript"> $(document).ready(function() { $('#IdSize').change(function () { var idS = $('#IdSize').val(); var urlPrice = '@Url.Action("GetPriceForSize")'; $.post(urlPrice, { idProd: '@Model.Product.IdProduct', idSize: idS }, function(data) { ............................ }); }); 

This is the partitial view GetPriceForSize:

 [HttpPost] public ActionResult GetPriceForSize(int idProd, int idSize) { Prices price = repository.Prices. Where(p => p.IdProd == idProd && p.IdSize == idSize). SingleOrDefault(); return Json(new { price = price.Price }); } @model haengematten.eu.Models.Prices <span class="regular-price" id="product-price-67_clone"> <span class="price">@Model.Price</span> </span> 

Help me figure it out, just started studying jquery.

`

  • 2
    The normal question. Why close it? - oleg_ismaylov
  • I did not close it, I do not remember doing it. Thanks for the answers)))) - dev85

2 answers 2

Can not quite understand the question. But there is such a solution. Always use it.

 $.ajax({ type: "POST", // метод передачи данных скрипту url: "url", // адрес скрипта data: data, // передаваемые параметры (обычная POST/GET запись) beforeSend: function (html) {}, // то, что будет выполняться до передачи success: function (html) { // то, что будет выполняться после успешной передачи $("body").append(html); // в данном случае на страницу добавляется то, что было "выведено на печать" в скрипте, куда передавались данные } }); 

    it is much clearer when all the URLs are explicitly indicated, in my opinion it is better not to use server code in js. You can try instead of return Json(new { price = price.Price }); use return PartialView("GetPriceForSize", price); and if in $ .post dataType is set to html, then everything should work


    and instead of SingleOrDefault(); use FirstOrDefault(); , it will pull out only one value from the base, while the first one will pull out everything, and it will look for the first element in memory

    here is what SingleOrDefault()

     SELECT [t0].[Id], [t0].[Name], [t0].[Description] FROM [Sections] AS [t0] WHERE [t0].[Id] = @p0 

    but FirstOrDefault()

     SELECT TOP (1) [t0].[Id], [t0].[Name], [t0].[Description] FROM [Sections] AS [t0] WHERE [t0].[Id] = @p0