我尝试确认销售,我需要有关产品的总和信息..
我的看法
@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/