I am trying to do a project related to track daily expensives, what im trying to do now is to add the foreign key UserId to my Expensive table so i can relate different expensives for diferent Users, the thing is that i already tryied to assign the foreign key and it saved as null, now it dont save anything dont know what is happening, im using Asp.net Mvc with C# and Entity Framework

Model:

    public int TipoDespesaId { get; set; }

    public int DespesaId { get; set; }

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

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

    public int TipoPagamentoId { get; set; }

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

    public TipoDespesa TipoDespesa { get; set; }

    public TipoPagamento TipoPagamento { get; set; }

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

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

    public string ApplicationUserId { get; set; }

Controller:

public ActionResult Create([Bind(Include = "DespesaId,TipoDespesaId,DespesaDescricao,DespesaValor,TipoPagamentoId,Data,Comentario")] Despesa despesa)
    {
        if (ModelState.IsValid)
        {
            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);
    }

View:

@Html.AntiForgeryToken()

@Html.HiddenFor(model => model.ApplicationUserId)

<div class="form-horizontal">
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.TipoDespesaId, "Tipo de Despesa", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TipoDespesaId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.TipoDespesaId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DespesaDescricao, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DespesaDescricao, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DespesaDescricao, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.DespesaValor, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.DespesaValor, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.DespesaValor, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.TipoPagamentoId, "Tipo de Pagamento", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("TipoPagamentoId", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.TipoPagamentoId, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Data, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Data, new { htmlAttributes = new { @class = "form-control datepicker" } })
            @Html.ValidationMessageFor(model => model.Data, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Comentario, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.Comentario,8,40, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Comentario, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
</div>

}

If someone can help me i appreciate.

Related posts

Recent Viewed