获取与UserId关联的下拉列表中的值

时间:2016-03-23 20:16:20

标签: asp.net razor

我的应用程序基本上控制了一个人的费用和收入,而且费用与支出类型相关,这些类型的支出可以由用户创建,并且必须因用户而异。 对支出类型进行最佳示例可以有多种选择,我只能选择一种,这是通过下拉列表实现的。 每个用户的支出类型和他们彼此不同的意图是什么,我有以下几点:

模型费用:

public class Expense
{
    public int TipeExpenseId { get; set; }

    public int ExpenseId { get; set; }

    [Display(Name = "Descrição da Despesa")]
    [Required]
    public string ExpenseDescription { get; set; }

    [Display(Name = "Valor")]
    [Required]
    public decimal ExpenseValue { get; set; }

    public int PayementTypeId { get; set; }

    [Display(Name = "Data")]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
    [Required]
    public DateTime Data { get; set; }

    public ExpenseTipe ExpenseTipe { get; set; }

    public PaymentType PaymentType { get; set; }

    [Display(Name = "Comentário")]
    public string Coment { get; set; }

    [ForeignKey("ApplicationUserId")]
    public virtual ApplicationUser User { get; set; }

    public string ApplicationUserId { get; set; }

}

Model TipeExpense:

public int ExpenseTipeId { get; set; }
[Display(Name = "Tipo de Despesa")]
[Required]
public string ExpenseTipeName { get; set; }

public virtual ApplicationUser User { get; set; }

public string ApplicationUserId { get; set; }

我想获得与创建它的用户相关的所有ExpenseTipe,我想我需要在linq查询中执行一些操作,以获取所有费用清单,而只是具有相同ID的费用作为用户但我不知道如何在Linq查询中执行此操作,这是我的控制器填充DropDown列表。

public ActionResult Create([Bind(Include = "DespesaId,TipoDespesaId,DespesaDescricao,DespesaValor,TipoPagamentoId,Data,Comentario")] Despesa despesa)
{
    if (ModelState.IsValid)
    {
        despesa.ApplicationUserId = User.Identity.GetUserId();
        db.Despesas.Add(despesa);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    ViewBag.TipoDespesaId = new SelectList(db.TipoDespesas, "TipoDespesaId", "TipoDespesaNome", despesa.TipoDespesaId);
    ViewBag.TipoPagamentoId = new SelectList(db.TipoPagamentos, "TipoPagamentoId", "TipoPagamentoNome", despesa.TipoPagamentoId);
    return View(despesa);
}

我认为我需要viewBag中的where条件,但不知道该怎么做:S

1 个答案:

答案 0 :(得分:0)

只需在db.TipoDespesas中添加where子句。

db.TipoDespesas.where(x=> x.applicationUserid == UserId)