c# - 从我的 Controller 获取信息到 onclick 确认(警报)

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

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

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

有什么帮助吗?

最佳答案

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

关于c# - 从我的 Controller 获取信息到 onclick 确认(警报),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038592/

相关文章:

c# - 在 WebBrowser 控件中禁用滚动

C# Entity Framework : Rename table field without losing data, 使用自动迁移

javascript - 窗口加载函数完全加载图像吗?

c# - ASP.NET MVC5 中如何通知用户提交的文件太大?

asp.net-mvc - 使用 MVC ASP.NET 将数据插入多个表

c# - 验证 Word 2007 模板文件

c# - 跨线程操作无效 : Control 'listBox1' accessed from a > thread other than the thread it was created on

c# - 如何使用 structuremap asp.net mvc 注册可选装饰器或带有可选参数的装饰器?

jquery - 如何在jquery中创建一个全局函数,并从另一个加载的页面调用它

jquery - 谷歌分析是否计算 Ajax 请求?