创建数据后如何获得响应?所以我想要的是何时保存。它显示它的响应,也许在消息框中?有可能吗?
这是我保存的控制器代码..
[HttpPost]
[ValidateAntiForgeryToken]
public async System.Threading.Tasks.Task<ActionResult> Create(FormCollection formCollection, string fn, string ln , ParentModel apsp)
{
string username = "sa";
string apiKey = "sa";
string baseUrl = "https://sandbox-api.paysimple.com";
var settings = new PaySimpleSdk.Models.PaySimpleSettings(apiKey, username, baseUrl);
var paymentService = new PaymentService(settings);
fn = apsp.Customer.FirstName;
ln = apsp.Customer.LastName;
string street1 = apsp.Customer.BillingAddress.StreetAddress1;
string street2 = apsp.Customer.BillingAddress.StreetAddress2;
string city = apsp.Customer.BillingAddress.City;
Enum statecode = apsp.Customer.BillingAddress.StateCode;
Enum country = apsp.Customer.BillingAddress.Country;
string zipcode = apsp.Customer.BillingAddress.ZipCode;
string credit = apsp.CreditCard.CreditCardNumber;
string expir = apsp.CreditCard.ExpirationDate;
Enum issuer = apsp.CreditCard.Issuer;
decimal amount = apsp.Payment.Amount;
string ccv = apsp.Payment.Cvv;
var customerPayment = new NewCustomerPayment<CreditCard>
{
Customer = new Customer()
{
FirstName = fn,
LastName = ln,
BillingAddress = new Address
{
StreetAddress1 = street1,
StreetAddress2 = street2,
City = city,
StateCode = (StateCode)statecode,
Country = (CountryCode)country,
ZipCode = zipcode
}
},
Account = new CreditCard
{
CreditCardNumber = credit,
ExpirationDate = expir,
Issuer = (Issuer)issuer
},
Payment = new Payment
{
Amount = amount,
Cvv = ccv
}
};
var newCustomerPayment = await paymentService.CreateNewCustomerPaymentAsync(customerPayment);
return RedirectToAction("Index");
}
创建数据的功能来自SDK和模型本身
答案 0 :(得分:2)
要显示您的操作反馈,您可以从您的方法返回JsonResult并进行调用并通过Javascript在浏览器端创建消息框。
采取的行动
1)在方法定义
中将ActionResult更改为JsonResultpublic async System.Threading.Tasks.Task<JsonResult> Create(FormCollection formCollection, string fn, string ln , ParentModel apsp)
2)将回报改为:
return this.Json(message)
3)使用ajax调用方法,并在回调方法
上创建消息框答案 1 :(得分:2)
您有两个选择:
创建操作应返回JsonResult
而不是重定向。但是在调用Create action时需要使用AJAX。:
public async System.Threading.Tasks.Task<ActionResult> Create(FormCollection formCollection, string fn, string ln , ParentModel apsp)
{
/// your code for creating object
return Json(newCustomerPayment, JsonRequestBehavior.AllowGet);
}
客户端使用Ajax.BeginForm
代替Html.BeginForm
using (Ajax.BeginForm("Create", Home , new AjaxOptions { HttpMethod = "POST", OnSuccess = "customerPaymentCreatedSuccess" }))
{
}
<script>
function customerPaymentCreatedSuccess(response)
{
alert(JSON.stringify(response, null, 4));
}
</script>
使用发布/重定向/获取模式。创建新付款后,将其存储在TempData中 并按照您目前的方式返回重定向:
TempData["newCustomerPayment"] = newCustomerPayment;
return RedirectToAction("Index");
然后在Index Action中检查TempData中是否有任何内容,如果有,请将其传递给视图
public ActionResult Index()
{
var customerPayment = TempData["newCustomerPayment"] as NewCustomerPayment;
if(customerPayment != null)
{
ViewBag.customerPayment = customerPayment;
}
//other code..
}
索引视图 - 生成显示customerPayment的JavaScript代码:
@{var customerPayment = ViewBag.customerPayment as NewCustomerPayment;}
@if(customerPayment != null)
{
<script>
alert("@Html.Raw(JsonConvert.SerializeObject(customerPayment ))");
</script>
}