class Account
{
int account_id;
String account_number;
decimal balance;
}
我有一个与交易相关的模型。
public class MakeTransactionModel
{
[Required]
public String AccountFrom { get; set; }
[Required]
public String AccountTo { get; set; }
public Decimal OrignalBalance { get; set; }
[Required]
public Decimal Amount { get; set; }
[Required]
public String TransactionFor { get; set; }
}
然后在控制器中,我将账户放入ViewBag中。
ViewBag.account_from = new SelectList(db.Accounts, "account_id", "account_number");
在View中,我创建了一个下拉菜单,用于显示所有账户。
@Html.DropDownListFor(u => u.AccountFrom, (SelectList)ViewBag.account_from, htmlAttributes: new { @class = "form-control", @id = "AccountFrom", onchange=@"
@Model.OrignalBalance = 1000; // I tried this but did not work
" })
现在,我正在尝试在EditorFor中显示所选账户的余额。
@Html.EditorFor(model => model.OrignalBalance, new { htmlAttributes = new { @id="OrignalBalance", @class = "form-control", disabled = "disabled", @readonly = "readonly" } })
我有所有的账户在 ViewBag 中,并在下拉菜单中显示这些账户的编号(这些账户也有余额)。我尝试在 DropDownFor 值改变时更改 EditorFor 的值,但仍然无法实现。我尝试使用 jquery 来实现,但不知道是否可以在 jquery 中使用 LINQ。
我的 jquery 代码如下:
<script type="text/javascript">
$(document).ready(function () {
$(function () {
$('#AccountFrom').change(function () {
var selectedValue = $('#AccountFrom').text();
$('#OrignalBalance').val(@{new BankEntities().Accounts.SingleOrDefault(acc => acc.account_number == $('#AccountFrom').text())}); // I am trying to do this
});
});
}
)
</script>
如果我能找到一个好的解决方案,就可以在更改事件上更新EditorFor。谢谢。