0

I have a jQuery plugin "Slider", and this slider show current price item, and I want to add posibility to change prices data by jQuery slider and update it at data base.

There is a model:

public class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Price { get; set; }
}

Controller, where I added method "SetPrice", for update and save data from ajax post method. But I do not know, it is correct way or not for getting data from javascript.

 public class ItemsController : Controller
{
    private ItemsContext db = new ItemsContext();

    [HttpGet]
    public ActionResult Index()
    {
        var items = db.Items.ToList();
        return View(items);
    }

    [HttpGet]
    public ActionResult Details(int? id)
    {
        if (id == null)
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        var item = db.Items.Where(i => i.Id == id).FirstOrDefault();
        return View(item);
    }

    public void SetPrice(Item item)
    {
        if (item == null)
            throw new Exception("Some exception");
        db.Items.Add(item);
        db.SaveChanges();
    }
}

}

View "Details" where I show current data item by slider, and want to add a logic for change data Price slider.

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<link rel="stylesheet" href="http://cdn.jsdelivr.net/jquery.roundslider/1.0/roundslider.min.css">
<script src="http://cdn.jsdelivr.net/jquery.roundslider/1.0/roundslider.min.js"></script>

<script src="~/CustomScripts/SliderJs.js"></script>

<div id="slider"></div>


    <script>
        var myApp = myApp || {};
        myApp.item = @Model.Price
    </script>
 <script src="~/CustomScripts/SliderJs.js"></script>
   

<h2>Details</h2>

<div>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Name)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Name)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Price)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Price)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Back to List", "Index")
</p>

This is how it looks at Details view.

enter image description here

And section scripts.

I changed from this:

(function ($, window, myApp) {
    $(document).ready(function () {
        $("#slider").roundSlider({
            radius: 90,
            width: 10,
            handleSize: "+10",
            sliderType: "range",
            value: myApp.item
        });
    });
})(jQuery, window, myApp);

To this, added an ajax post method but something goes wrong, even slider don`t show current date.

(function ($, window, myApp) {
    $(document).ready(function () {
        $("#slider").roundSlider({
            radius: 90,
            width: 10,
            handleSize: "+10",
            sliderType: "range",
            value: myApp.item,
        });
            $.ajax({
                url: 'Items/SetPrice',
                type: 'POST',
                data: { value: value},
                contentType: 'application/json',
                dataType: "json",
            })
        });
    });
})(jQuery, window, myApp);

I`m looking for any advices, how to change properly a method "SetPrice" and script. Thank you so much for your time.

1 Answer 1

1

Regarding this documentation I guess you should subscribe to the change event and post your value to the server (using ajax there). You should also use Price instead of value when sending data back to object. Otherwise it would not bind to your Item object in SetPrice action. Also think about how you will find the proper Item to update the price. You should probably add an id along with price.

So that your slider initialization should look somehow like this (note I have not tested that):

$("#slider").roundSlider({
    radius: 90,
    width: 10,
    handleSize: "+10",
    sliderType: "range",
    value: myApp.item,
    change: function(value)
    {
        $.ajax({
            url: 'Items/SetPrice',
            type: 'POST',
            data: { Price: value},
            contentType: 'application/json',
            dataType: "json",
        })
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thank you for answer, to update item price I have tried this, <dd id="Price" data-price="@Model.Price"> @Html.DisplayFor(model => model.Price) </dd> but it`s not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.