【问题标题】:Getting information from my controller into onclick confirm(alert)从我的控制器获取信息到 onclick 确认(警报)
【发布时间】:2012-10-23 20:20:27
【问题描述】:

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

我的观点

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

有什么帮助吗?

【问题讨论】:

  • GetOWnerPrice 应该是 JsonAction 而不是 ActionResult。关于返回的 json,我不确定它是否像您的想法,因为您将 SalePrice 传递给 ownerPrice
  • 10x 我会尝试它,但它在 actionresult 之前可以工作。现在我不知道如何在我的虚拟机或确认消息中使用这个 ownerprice(提交-onclick)
  • 为什么是 json?为什么不做一个正常形式的帖子? onclick="... 是怎么回事?这对我来说看起来不合法。
  • onclick 完全不应该在那里......
  • 因为我想在发布之前发出警报,如果经销商看到他的成本高于他出售它 - 他会点击取消,我们不会发布它..

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


【解决方案1】:

根据您编写它的方式,@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; }
});

【讨论】:

  • 我做了两个第一部分,但在第三部分我尝试把它放在 evryware 但没有成功.. 总结如下: 也许你可以请具体写在哪里?对不起我的英语不好=)
  • 在这个模型中,一旦价格已知,您可能会在数量发生变化后立即进行更新调用。另一种选择是在按钮的 onclick 处理程序中进行调用,如: onclick='$.ajax(...).success(function(price) { if (confirm(...) }
【解决方案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);
    }

【讨论】:

    【解决方案3】:

    您是否尝试了 $.ajaxasync = false? 它将控制权返回到您的点击处理程序。比如:

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

    【讨论】:

    • 关于“onclick”事件?我尝试了一下,但它对我有用.. 10x
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2018-09-26
    相关资源
    最近更新 更多