Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting information from my controller into onclick confirm(alert)

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?

like image 797
oCcSking Avatar asked Oct 23 '12 20:10

oCcSking


People also ask

How do you return a message from a controller?

We can return the custom message from controller by throwing exception and handling it at client side using the ActionFailure event of the Grid. Grid Rendering Code. 2. Handle the returned message in the ActionFailure event of the Grid.

How can create confirm box in MVC controller?

You dont create confirm box in a Controller, but yes in a View, using JQuery Dialog. The Controller is already inside the server, so you don't have user interactions there. Your View, in the other hand, is the place where the user will choose options, type information, click on buttons etc...

What is Onclick alert?

A JavaScript can be executed when an event occurs, the user clicks on any HTML tag elements. The onclick and alert events are most frequently used event type in the JavaScript for web pages. If any anonymous function to the HTML elements the onclick attribute will attach event to this element.

How can show alert message in button click in MVC?

FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST. The Form consists of two elements i.e. a TextBox and a Submit Button. The ViewBag object named Message is checked for NULL and if it is not NULL then the value of the object is displayed using JavaScript Alert MessageBox.


3 Answers

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; }
});
like image 57
Heather Avatar answered Oct 05 '22 11:10

Heather


finnaly i take the two answare =)

my view:

@model oCc.IPToGo.ViewModel.CardSaleViewModel

@{
   ViewBag.Title = "Card Sale";
   var ownerDealerPrice = 0;
}

  @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)
        @Html.ValidationMessageFor(model => model.CardModel.SerialNumber)
    </div>

      <p>
        <input type="submit" value="CardSale" />
    </p>

</fieldset>
}           
<script type="text/javascript">
$(document).submit(function() 
{
    var serial = "";
    serial = $("#CardModel_SerialNumber").val();
    var uurl = "";
    uurl = "@Url.Action("GetOwnerPrice","Card")/"+serial;

     $.ajaxSetup({ cache: false });

    $.ajax({ async: false , url: uurl,  dataype : 'json', method : 'GET'})
    .success(function (price) {ownerDealerPrice = price; 

    $.ajaxSetup({ cache: true }); 
   });
 return (confirm('The owner dealer price is : ' + ownerDealerPrice + '. Are you sure?'))
});

the controller code

     public ActionResult GetOwnerPrice(string ID)
    {
        var currentDealer =  db.Dealers.Single(o => o.UserName == User.Identity.Name);
        var owner = db.Dealers.Single(s => s.DealerID ==currentDealer.OwnerDealerID);
        var card = db.Cards.Single(s => s.SerialNumber == ID);
        var ownerPrice = owner.ProductToSale.Single(pr => pr.ProductID == card.ProductID).SalePrice;

        return Json(ownerPrice, JsonRequestBehavior.AllowGet);
    }
like image 41
oCcSking Avatar answered Oct 05 '22 12:10

oCcSking


Did you try $.ajax with async = false? It will return control right into your click handler. Something like:

var tmp = 0;
$.ajax({ 
   async = false, 
   url = ..., 
   dataype = 'json',
   method = 'POST'})
   .complete(function(data) {
      tmp = data
   });
if(confirm("my message here: " + tmp)) {... }
like image 38
Eugene Y. Avatar answered Oct 05 '22 10:10

Eugene Y.