Well im doing a application where i have a model like this
public class Saldo
{
public Saldo()
{
Expenses = new List<Expese>();
Incomes = new List<Income>();
}
public int SaldoId { get; set; }
public List<Expense> Despesas { get; set; }
public List<Income> Rendimentos { get; set; }
public string ApplicationUserId { get; set; }
}
what i want to do is add a single expense to the list but it is not working, when i output on the console the count it is always null in the controller im doing this:
public ActionResult Create([Bind(Include = "ExpenseId,TipoDespesaId,DespesaDescricao,DespesaValor,TipoPagamentoId,Data,Comentario")] Expense expense)
{
var userId = User.Identity.GetUserId();
if (ModelState.IsValid)
{
var balance = db.Balance.Where(d => d.ApplicationUserId == userId).FirstOrDefault();
expense.ApplicationUserId = userId;
if (balance == null)
{
Balance s = new Balance();
s.Expense.Add(expense);
s.ApplicationUserId = userId;
db.Balance.Add(s);
}
else
{
Balance.Expense.Add(expense);
}
db.Expense.Add(expense);
db.SaveChanges();
return RedirectToAction("Index");
}
The expense comes from the form and is passed to the Action
I created a new instance of Balance it worked adding the ApplicationUserId but the expense on the list didnt work, can someone explain me why it happen?
Ps: Sorry for my bad english