【发布时间】:2016-04-02 19:32:52
【问题描述】:
我正在尝试制作一个在线银行网站(用于学习 ASP.NET MVC)。我有一个类 Account
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");
在视图中,我创建了一个下拉菜单来显示所有帐户
@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。
谢谢。
【问题讨论】:
标签: c# jquery asp.net-mvc linq asp.net-mvc-4