title: Only defualt values passed to database
body: <p>I'm pretty new to programming and I'm strugling with quite simple "Create" controller and view.</p>
  
  <p>In the view user is declaring values, which should be passed to database. Here's the model:</p>
  
  <pre><code>public class Expense
  
  {
      public int ExpenseID { get; set; }
  
      [DisplayName("Data")]
      [Column(TypeName = "DateTime2")]
      public DateTime Date { get; set; }
  
      [DisplayName("Wartość")]
      public decimal Amount { get; set; }
  
      [DisplayName("Opis")]
      public string Details { get; set; }
  
      [DisplayName("Rodzaj")]
      public int CategoryID { get; set; }
  
      public virtual Category Category { get; set; }
  
  }
  
  public class Category
      {
          public int CategoryID { get; set; }
  
          public string Name { get; set; }
  
          public virtual ICollection&lt;Expense&gt; Expenses { get; set; }
      }
  </code></pre>
  
  <p>In the same view I want to include partial view for managing the categories (adding, removing).</p>
  
  <p>To have this working I've implemented ViewModel:</p>
  
  <pre><code> public class ExpenseCreateViewModel
  {
      public Expense ExpenseCreate { get; set; }
      public Category CategoryCreate { get; set; }
  }
  </code></pre>
  
  <p>And here's the code for my View:</p>
  
  <pre><code>@model Wydatki2._0.Models.ExpenseCreateViewModel
  
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Dodaj wydatek&lt;/h2&gt;
  
  
  &lt;table class="table"&gt;
  &lt;tr&gt;
      &lt;th&gt;
          &lt;div&gt;
              @using (Html.BeginForm("CreateExpense", "ExpenseCreate", FormMethod.Post, new { }))
              {
                  @Html.AntiForgeryToken()
  
                  &lt;div class="form-horizontal"&gt;
  
                      @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.ExpenseCreate.CategoryID, "Rodzaj", htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
  
                              @Html.DropDownList("CategoryID", null, htmlAttributes: new { @class = "form-control" })
                              @Html.ValidationMessageFor(model =&gt; model.ExpenseCreate.CategoryID, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.ExpenseCreate.Amount, htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
                              @Html.EditorFor(model =&gt; model.ExpenseCreate.Amount, new { htmlAttributes = new { @class = "form-control" } })
                              @Html.ValidationMessageFor(model =&gt; model.ExpenseCreate.Amount, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.ExpenseCreate.Date, htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
                              @Html.EditorFor(model =&gt; model.ExpenseCreate.Date, new { htmlAttributes = new { @class = "form-control" } })
                              @Html.ValidationMessageFor(model =&gt; model.ExpenseCreate.Date, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="form-group"&gt;
                          @Html.LabelFor(model =&gt; model.ExpenseCreate.Details, htmlAttributes: new { @class = "control-label col-md-2" })
                          &lt;div class="col-md-10"&gt;
                              @Html.EditorFor(model =&gt; model.ExpenseCreate.Details, new { htmlAttributes = new { @class = "form-control" } })
                              @Html.ValidationMessageFor(model =&gt; model.ExpenseCreate.Details, "", new { @class = "text-danger" })
                          &lt;/div&gt;
                      &lt;/div&gt;
  
  
  
                      &lt;div class="form-group"&gt;
                          &lt;div class="col-md-offset-2 col-md-10"&gt;
                              &lt;input type="submit" value="Dodaj" class="btn btn-default" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              }
          &lt;/div&gt;
      &lt;/th&gt;
  &lt;th&gt;
      &lt;div&gt;
          &lt;input type="button" id="btn" class="btn btn-default" value="Dodaj/Usuń Kategorię" /&gt;
          &lt;p class="error"&gt;@ViewBag.Warning&lt;/p&gt;
      &lt;/div&gt;
  
      &lt;div id="Create" style="display:none"&gt;
          @Html.Partial("CreateCategory", Model.CategoryCreate)
      &lt;/div&gt; 
  
  
  &lt;/th&gt;
  &lt;/tr&gt;
  &lt;/table&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
      @Scripts.Render("~/bundles/toggle")
  }
  </code></pre>
  
  <p>The problem is that when I submit the form only the CategoryID value is passed correctly. The rest of parameters have just default values. The partial view is working well - I'm able to add or remove categories, which then are passed to the main view.</p>
  
  <p>The code for my Controller:</p>
  
  <pre><code>namespace Wydatki2._0.Controllers
  {
      public class ExpenseCreateController : Controller
      {
          private WydatkiContext db = new WydatkiContext();
  
  
  
  
          public ActionResult Create(bool? warn = false)
          {
              ExpenseCreateViewModel model = new ExpenseCreateViewModel()
              {
                  ExpenseCreate = new Expense(),
                  CategoryCreate = new Category()
              };
  
              var query = from b in db.Categories
                          where b.CategoryID != 1
                          select b;
  
  
  
              if (warn.GetValueOrDefault())
              {
                  ViewBag.Warning = "Nie możesz usunąć tej kategorii.";
              }
  
              ViewBag.CategoryID = new SelectList(query, "CategoryID", "Name");
              return View(model);
          }
  
  
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult CreateExpense([Bind(Include = "ExpenseID,Date,Amount,Details,CategoryID")] Expense expense)
          {
              if (ModelState.IsValid)
              {
                  db.Expenses.Add(expense);
                  db.SaveChanges();
  
                  return RedirectToAction("Create");
              }
  
              var query = from b in db.Categories
                          where b.CategoryID != 1
                          select b;
  
              ViewBag.CategoryID = new SelectList(query, "CategoryID", "Name", expense.CategoryID);
              return View(expense);
          }
  
  
  
  
          public ActionResult CreateCategory()
          {
  
              return View();
          }
  
  
  
  
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult CreateCategory([Bind(Include = "CategoryID,Name")] Category category)
          {
  
  
              if (ModelState.IsValid)
              {
                  Category cat = db.Categories.FirstOrDefault(c =&gt; c.Name== category.Name);
  
  
  
                  if (cat != null)
                  {
                      if (cat.CategoryID == 1 || cat.CategoryID ==2)
  
                      {
                          return RedirectToAction("Create", new { warn = true });
                      }
                      else
                      {
                          db.Categories.Remove(cat);
                          db.SaveChanges();
                          return RedirectToAction("Create");
                      }
                  }
  
                  else
                  {
  
                      db.Categories.Add(category);
                      db.SaveChanges();
  
                      return RedirectToAction("Create");
                  }
              }
  
              return View(category);
          }
  </code></pre>
  
  <p>I'm inclined to believe that the problem is caused by the model, which I'm passing to the view, but I really don't know, how to pass it correctly... Anyone could help with this?</p>