Getting information from my controller into onclick confirm(alert)

asp.net-mvc, c#, jquery, razor

Solution

The way in which you have written it, the @Model.OwnerPrice value must be known during page generation, since the template engine only has values that exist in your model when it does the template-data merge.

If you do know this value during page load, then simply use the value in your model exactly as you have, and if the user ever gets to this dialog, they will see the proper value.

If the value of this confirmation dialog is not known during page load, then you have the right idea to retrieve it via an Ajax call. The trick is to update the DOM with the new information when the call finishes. You can do this in three steps. First, change your dialog box:

First:

if (confirm('The owner dealer price is : ' + ownerDealerPrice + '. Are you sure?'))

Second: Declare a new global variable:

var ownerDealerPrice;

Third: Get the price:

$.ajax({ url: "/GetOwnerPrice", type: "GET", data: "serial=" + serial })
.success(function (price) {ownerDealerPrice = price; }
});

Problem

i try to confirm a sale and i need sum information about the product.. my View ``` @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Card</legend> <div class="editor-label"> @Html.LabelFor(model => model.CardModel.SerialNumber, "Serial No") </div> <div class="editor-field"> @Html.EditorFor(model => model.CardModel.SerialNumber, new { id = "sn" }) @Html.ValidationMessageFor(model => model.CardModel.SerialNumber) </div> <p> <input type="submit" value="CardSale" id="btnSubmit" onclick="if (confirm('The owner dealer price is : **@Model.OwnerPrice** . Are you sure?')) { return true; } else { return false; }" /> </p> </fieldset> } ``` i tryed with the ' $.getJSON ' like this: ``` <script type="text/javascript"> $(document).ready(function() { $("#btnSubmit").click(function () { var serial = $("#sn"); var price = ""; var url = ""; url = "@Url.Action("GetOwnerPrice","Card")/"+serial; $.getJSON(url,function(data) { alert(data.Value); }); }); }); ``` and on the controller ``` public ActionResult GetOwnerPrice(int sn) { var owner = db.Dealers.Single(s => s.DealerID == db.Dealers.Single(o => o.UserName == User.Identity.Name).OwnerDealerID); var ownerPrice = owner.ProductToSale.Single(pr => pr.ProductID == sn).SalePrice; return Json(ownerPrice, JsonRequestBehavior.AllowGet); } ``` but i dont know how to return it to the onclick confirm msg or into my ViewModel.. any help?

Original source