I'm trying to create a web page that displays the columns from a table in SQL Server. Several of the columns must be have select lists and the value of several columns must change depending on the selected item.
As an example, I have a table called Accounts that has an AccountNumber and AccountName column. I want the AccountName cell on the web page to change depending on the AccountNumber selection from the select list. I've tried using JavaScript and jQuery to pass the AccountNumber to a C# method that gets the AccountName, but when I make a selection and debug I get an error saying that the parameter the method received can't be null.
JavaScript
function getAccountNumber(x) {
var selectedAccountNumber = document.getElementById(x.id).value;
var isNumber = isNaN(selectedAccountNumber);
if (!isNumber) {
var accountNumber = parseInt(selectedAccountNumber, 10);
var accountName = $.post('@Url.Action("GetAccountName", "Statements")', accountNumber);
alert(accountName);
}
}
C#
public string GetAccountName(int ID)
{
var item = db.Accounts.Where(acc => acc.AccountNumber == ID).Select(acc => acc.AccountName).Single();
return item;
}
I don't know why it's not passing the parameter properly.