asp.net之如何在 ASP.Net 中保存数据后获得 Json 响应
shanyou
阅读:21
2025-01-19 22:14:33
评论:0
创建数据后如何获得响应?所以我想要的是什么时候保存。它显示它的响应,也许在消息框中?可以吗?
这是我保存的 Controller 代码..
[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和模型本身
请您参考如下方法:
你有两个选择:
创建操作应该返回
JsonResult
而不是重定向。但是在调用 Create 操作时需要使用 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>
使用 Post/Redirect/Get 模式。创建新付款后,将其存储在 TempData 中 并像现在一样返回重定向:
TempData["newCustomerPayment"] = newCustomerPayment; return RedirectToAction("Index");
然后在 Index Action 中检查 TempData 中是否有任何内容,如果有,则将其传递给 View
public ActionResult Index() { var customerPayment = TempData["newCustomerPayment"] as NewCustomerPayment; if(customerPayment != null) { ViewBag.customerPayment = customerPayment; } //other code..
索引 View - 生成 JavaScript 代码以显示 customerPayment:
@{var customerPayment = ViewBag.customerPayment as NewCustomerPayment;}
@if(customerPayment != null)
{
<script>
alert("@Html.Raw(JsonConvert.SerializeObject(customerPayment ))");
</script>
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。