从我的控制器获取信息到onclick确认(警报)

时间:2012-10-23 20:20:28

标签: c# jquery asp.net-mvc razor

我尝试确认销售,我需要有关产品的总和信息..

我的观点

@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>
}           

我尝试使用'$ .getJSON'这样:

<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);
        });
    });       
 });

并在控制器上

 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);
    }

但我不知道如何将其返回到onclick确认消息或我的ViewModel ..

任何帮助?

3 个答案:

答案 0 :(得分:3)

编写它的方式,必须在页面生成期间知道@ Model.OwnerPrice值,因为模板引擎在模板数据合并时只有模型中存在的值。

如果您在页面加载期间 知道此值,则只需使用模型中的值,如果用户进入此对话框,他们将看到正确的值。

如果在页面加载期间不知道此确认对话框的值,那么您有正确的想法通过Ajax调用检索它。诀窍是在调用结束时用新信息更新DOM。您可以分三步完成此操作。首先,更改对话框:

首先:

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

第二: 声明一个新的全局变量:

var ownerDealerPrice;

第三: 得到价格:

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

答案 1 :(得分:2)

最后我接受了两个回答=)

我的观点:

@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?'))
});

控制器代码

     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);
    }

答案 2 :(得分:1)

您是否尝试使用 async = false $。ajax ? 它会将控制权返回到您的点击处理程序中。类似的东西:

var tmp = 0;
$.ajax({ 
   async = false, 
   url = ..., 
   dataype = 'json',
   method = 'POST'})
   .complete(function(data) {
      tmp = data
   });
if(confirm("my message here: " + tmp)) {... }