[英]Update @Model using jQuery and diffrent controller/function
我將嘗試解釋它,因為它有點復雜:我正在嘗試創建一個區塊鏈錢包,我使用 AccountController 成功加載了我的視圖。 在那里我有錢包余額,一切都很好。
但有時我想轉賬(=更改錢包余額)我的視圖使用從 AccountController(Account 類)返回的 @Model 讀取余額。
我的問題是更新@Model。 從我所做的 - 到目前為止沒有任何改變。
在發布收件人地址和資金以轉回服務器后,使用 jQuery(按下按鈕)將其直接轉入不同的 controller,我需要將 @Model 返回到具有更新余額的視圖,該視圖將被直接調用到 Z4C4AD5FCA02E7A8AAZDBEDB已經讀過它的元素。 在服務器中,我已經擁有具有相同詳細信息的帳戶實例。
這是我的代碼:
HTML:
<label id="ETH-Value">@Model.AccountBalance ETH</label>
<table>
.....
<tr >
<td style="">
<input name="RecipientAmmountInput" type="number" required="required" id="RecipientAmmountInput" placeholder="Enter Ammount" min="0" max=@Model.AccountBalance>
</td>
<td ">
<a style="color:blue;" href="javascript:void(0);" onclick="SendMoneyTransaction()" id="nextButton"><strong>Next></strong></a>
</td>
</tr>
</table>
jQuery:
function SendMoneyTransaction()
{
var recipientAddress = $('#RecipientAddressInput').val();
var tranferValueETH = $('#RecipientAmmountInput').val();
.....
var url = "/SendDepositETH/SendMoney";
$.post(url, { AddressTo: recipientAddress, Amount: tranferValueETH }, function (data)
{
//The post is working
...
})
C#(服務器):
...
public class SendDepositETHController : Controller
{
[HttpPost]
public IActionResult SendMoney(String AddressTo, String Amount)
{
double amountToSend = Convert.ToDouble(Amount);
Account transferFrom = AccountController.myAccount;
transferFrom.AccountBalance = amountToSend; //for example
return View(transferFrom);
}
}
....
在 controller 中,您可以:返回 transferFrom; 並將 IActionResult 更改為 Account。 在視圖中,
$.post(url, { AddressTo: recipientAddress, Amount: tranferValueETH }, function (data)
{
//The post is working
// Something like this
$("#ETH-Value").val(data.AccountBalance);
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.