在modeldata有效后运行javascript代码
我正在开发一个facebook应用程序,我有一个用户填写的表格,当它提交时,我想通过facebook画布付款向他们收费。当然,这意味着modeldata首先必须是有效的,但我一直认为mvc代码只能在javascript代码调用它之后运行。我将如何在下面的mvc方法中调用此javascript命令在modeldata有效后运行javascript代码,javascript,c#,asp.net-mvc,facebook,facebook-payments,Javascript,C#,Asp.net Mvc,Facebook,Facebook Payments,我正在开发一个facebook应用程序,我有一个用户填写的表格,当它提交时,我想通过facebook画布付款向他们收费。当然,这意味着modeldata首先必须是有效的,但我一直认为mvc代码只能在javascript代码调用它之后运行。我将如何在下面的mvc方法中调用此javascript命令 function buy() { var obj = { method: 'pay', action: 'purchaseitem',
function buy() {
var obj = {
method: 'pay',
action: 'purchaseitem',
product: 'https://mywebsite.net/product.html'
};
FB.ui(obj, function(data) {
console.log(data);
});
}
**document.getElementById('submitbtn').onclick = function() {buy()};** // trying to only call this after model data is valid
};
[HttpPost]
public ActionResult Contact(Project.Models.Order c)
{
decimal price = 0;
if (ModelState.IsValid)
{
try
{
// call javascript code here to open the window
}
catch (Exception)
{
return View("Error");
}
}
return View("Index");
}
public async Task<ActionResult> Index(FacebookContext context)
{
if (ModelState.IsValid)
{
//var user = await context.Client.GetCurrentUserAsync<MyAppUser>();
//var client = new FacebookClient(context.AccessToken);
//dynamic me = client.Get("me");
//string name = me.Name;
loadDropDownLists();
return View();
}
return View("Error");
}
函数购买(){
var obj={
方法:'支付',
操作:“purchaseitem”,
产品:'https://mywebsite.net/product.html'
};
FB.ui(对象、功能(数据){
控制台日志(数据);
});
}
**document.getElementById('submitbtn')。onclick=function(){buy()};**/尝试仅在模型数据有效后调用此函数
};
[HttpPost]
公共行动结果联系人(Project.Models.Order c)
{
十进制价格=0;
if(ModelState.IsValid)
{
尝试
{
//在此处调用javascript代码以打开窗口
}
捕获(例外)
{
返回视图(“错误”);
}
}
返回视图(“索引”);
}
公共异步任务索引(FacebookContext上下文)
{
if(ModelState.IsValid)
{
//var user=await context.Client.GetCurrentUserAsync();
//var client=新的FacebookClient(context.AccessToken);
//动态me=客户机.Get(“我”);
//string name=me.name;
loadDropDownList();
返回视图();
}
返回视图(“错误”);
}
索引视图
@using HomeworkHelpers.Models
@using Microsoft.AspNet.Facebook.Models
@model HomeworkHelpers.Models.Order
@{
ViewBag.Title = "Home Page";
}
@section PageScripts{
<script type="text/javascript">
$(document).ready(function () {
$('#documentTypeList').change(function () {
var modelData = {
documentType: $('#documentTypeList').val(),
urgency: $('#urgencyList').val(),
numberOfPages: $('#numberOfPagesList').val()
};
$.ajax({
type: "GET",
data: modelData,
url: "/Home/getNewPrice",
async: true,
success: function (data) {
$('#priceLabel').html(data.currentPrice);
$('#UnFormattedPrice').val(data.unformattedCurrectPrice);
}
});
});
$('#numberOfPagesList').change(function () {
var modelData = {
documentType: $("#documentTypeList").val(),
urgency: $("#urgencyList").val(),
numberOfPages: $("#numberOfPagesList").val()
};
$.ajax({
type: "GET",
data: modelData,
url: "/Home/getNewPrice",
async: true,
success: function (data) {
$('#priceLabel').html(data.currentPrice);
$('#UnFormattedPrice').val(data.unformattedCurrectPrice);
}
});
});
$('#urgencyList').change(function () {
var modelData = {
documentType: $("#documentTypeList").val(),
urgency: $("#urgencyList").val(),
numberOfPages: $("#numberOfPagesList").val()
};
$.ajax({
type: "GET",
data: modelData,
url: "/Home/getNewPrice",
async: true,
success: function (data) {
$('#priceLabel').html(data.currentPrice);
$('#UnFormattedPrice').val(data.unformattedCurrectPrice);
}
});
});
$('.spacing').click(function () {
var select = $("#numberOfPagesList");
select.empty().append($('<option></option>').val('').text('--Select--'));
var url = '@Url.Action("NumberOfPagesList", "Home")';
$.getJSON(url, { id: $(this).val() }, function (data) {
$.each(data, function (i, item) {
select.append($('<option></option').val(item.Value).text(item.Name));
});
});
});
$('#currencyList').change(function () {
$.ajax({
type: "GET",
url: "/Home/changeCurrency?newCurrency=" + this.value,
async: true,
success: function (result) {
$('#priceLabel').html(result.currentPrice);
}
});
});
});
</script>
@if (Model.Buy)
{
<script>
$(document).ready(function () {
buy();
});
</script>
}
}
@using (Html.BeginForm("Contact", "Home", FormMethod.Post))
{
@Html.ValidationSummary(true)
<div class="row">
@Html.LabelFor(model => model.Name, "Name:")
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="row">
@Html.LabelFor(model => model.Email, "Email:")
@Html.EditorFor(model => model.Email, new { id = "email" })
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="row">
@Html.LabelFor(model => model.PhoneNumber, "Phone Number:")
@Html.EditorFor(model => model.PhoneNumber, new { id = "phoneNumber" })
@Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="row">
@Html.LabelFor(model => model.Topic, "Topic:")
@Html.EditorFor(model => model.Topic)
@Html.ValidationMessageFor(model => model.Topic)
</div>
<div class="row">
@Html.LabelFor(model => model.Subject, "Subject:")
@Html.DropDownListFor(model => model.Subject, (SelectList)ViewBag.subject, "--Select--", new { id = "subjectList" })
@Html.ValidationMessageFor(model => model.Subject)
</div>
<div class="row">
@Html.LabelFor(model => model.Style, "Style:")
@Html.DropDownListFor(model => model.Style, (SelectList)ViewBag.paperStyle, "--Select--", new { id = "paperStyleList" })
@Html.ValidationMessageFor(model => model.Style)
</div>
<div class="row">
@Html.LabelFor(model => model.DocumentType, "Document Type:")
@Html.DropDownListFor(model => model.DocumentType, (SelectList)ViewBag.documentType, "--Select--", new { id = "documentTypeList" })
@Html.ValidationMessageFor(model => model.DocumentType)
</div>
<div class="row">
@Html.LabelFor(model => model.AcademicLevel, "Academic Level:")
@Html.DropDownListFor(model => model.AcademicLevel, (SelectList)ViewBag.academicLevel, "--Select--", new { id = "academicLevelList" })
@Html.ValidationMessageFor(model => model.AcademicLevel)
</div>
<div class="row">
@Html.LabelFor(model => model.NumberOfPages, "Number of Pages/Words:")
@Html.DropDownListFor(model => model.NumberOfPages, (SelectList)ViewBag.numberOfPages, "--Select--", new { id = "numberOfPagesList" })
@Html.ValidationMessageFor(model => model.NumberOfPages)
</div>
<div class="row">
@Html.LabelFor(model => model.NumberOfSources, "Number of Sources:")
@Html.DropDownListFor(model => model.NumberOfSources, (SelectList)ViewBag.numberOfSources, "--Select--", new { id = "numberOfSourcesList" })
@Html.ValidationMessageFor(model => model.NumberOfSources)
</div>
<div class="row">
@Html.LabelFor(model => model.Urgency, "Urgency:")
@Html.DropDownListFor(model => model.Urgency, (SelectList)ViewBag.urgency, "--Select--", new { id = "urgencyList" })
@Html.ValidationMessageFor(model => model.Urgency)
</div>
<div class="row">
@Html.LabelFor(model => model.Spacing, "Spacing:")
@Html.RadioButtonFor(m => m.Spacing, "Double", new { @class = "spacing", id = "double", @Checked = "checked" }) Double
@Html.RadioButtonFor(m => m.Spacing, "Single", new { @class = "spacing", id = "single" }) Single
@Html.ValidationMessageFor(model => model.Spacing)
</div>
<div class="row">
@Html.LabelFor(model => model.Requirements, "Requirements:")
@Html.TextAreaFor(model => model.Requirements)
@Html.ValidationMessageFor(model => model.Requirements)
</div>
<div class="row">
@Html.DropDownListFor(model => model.Currency, (SelectList)ViewBag.currency, null, new { id = "currencyList" })
<h2 id="priceLabel">
@Html.DisplayFor(model => model.Price)
</h2>
@Html.HiddenFor(model => model.UnFormattedPrice)
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</div>
}
@使用家庭作业助手。模型
@使用Microsoft.AspNet.Facebook.Models
@模型家务助理.Models.Order
@{
ViewBag.Title=“主页”;
}
@第页脚本{
$(文档).ready(函数(){
$('#documentTypeList')。更改(函数(){
var模型数据={
documentType:$(“#documentTypeList”).val(),
紧急情况:$(“#紧急情况列表”).val(),
numberOfPages:$('#numberOfPagesList').val()
};
$.ajax({
键入:“获取”,
数据:模型数据,
url:“/Home/getNewPrice”,
async:true,
成功:功能(数据){
$('#priceLabel').html(data.currentPrice);
$('#unformatedPrice').val(data.unformatedCurrencPrice);
}
});
});
$('#numberOfPagesList')。更改(函数(){
var模型数据={
documentType:$(“#documentTypeList”).val(),
紧急情况:$(“#紧急列表”).val(),
numberOfPages:$(“#numberOfPagesList”).val()
};
$.ajax({
键入:“获取”,
数据:模型数据,
url:“/Home/getNewPrice”,
async:true,
成功:功能(数据){
$('#priceLabel').html(data.currentPrice);
$('#unformatedPrice').val(data.unformatedCurrencPrice);
}
});
});
$('#urgencyList')。更改(函数(){
var模型数据={
documentType:$(“#documentTypeList”).val(),
紧急情况:$(“#紧急列表”).val(),
numberOfPages:$(“#numberOfPagesList”).val()
};
$.ajax({
键入:“获取”,
数据:模型数据,
url:“/Home/getNewPrice”,
async:true,
成功:功能(数据){
$('#priceLabel').html(data.currentPrice);
$('#unformatedPrice').val(data.unformatedCurrencPrice);
}
});
});
$('.spating')。单击(函数(){
var select=$(“#numberOfPagesList”);
select.empty().append($('').val('').text('--select--');
var url='@url.Action(“NumberOfPagesList”,“Home”);
$.getJSON(url,{id:$(this).val()},函数(数据){
$。每个(数据、功能(i、项){
在模型验证成功时,选择.append($('将ViewBag.Buy属性值设置为true,并返回视图:
[HttpPost]
public ActionResult Contact(Project.Models.Order c)
{
decimal price = 0;
if (ModelState.IsValid)
{
try
{
ViewBag.Buy = true;
return View("Index", c)
}
catch (Exception)
{
return View("Error");
}
}
return View("Index");
}
在索引页的末尾添加以下java脚本代码:
@if (ViewBag.Buy != null && ViewBag.Buy == true)
{
<script>
$(document).ready(function () {
buy();
});
</script>
}
@if(ViewBag.Buy!=null&&ViewBag.Buy==true)
{
$(文档).ready(函数(){
购买();
});
}
我把它放在我的pagescripts部分,但是我得到了model.buy的对象引用错误。那么我做错什么了吗@MajoB@user3610374你能为连接操作方法和索引视图发布一个GET方法吗?我只有上面粘贴的Index方法。@user3610374我是说视图(“Index”)我在@MajoB上面添加了它
@if (ViewBag.Buy != null && ViewBag.Buy == true)
{
<script>
$(document).ready(function () {
buy();
});
</script>
}