title: pass @Html.EditorFor "Date" value back to Controller, manipulate value and pass back to view to populate Dropdown list
body: <p>I have the following MVC application, see front-end below:</p>
  
  <p><img src="https://i.stack.imgur.com/GReBV.png" alt="FinanceMVC screen - front-end"></p>
  
  <p>"Invoice Date Created" is a html5 compatable datepicker based on the following method: </p>
  
  <p><a href="http://www.aubrett.com/InformationTechnology/WebDevelopment/MVC/DatePickerMVC5.aspx" rel="nofollow noreferrer">http://www.aubrett.com/InformationTechnology/WebDevelopment/MVC/DatePickerMVC5.aspx</a> </p>
  
  <p>My requirement on screen load is the following:</p>
  
  <p>1) On datepicker "onSelect" of date and textbox updates, I need to pass this value e.g. 2014/11/19 and manipulate it to be in the format of yyyymm (e.g. 201411). There's a catch. It needs to be able create such a value for the current period, as well as the previous period. </p>
  
  <p>So, 2014/11/19 should populate "Invoice Number Period" Dropdown list with two options namely:</p>
  
  <p><strong>201411</strong> (for current period)<br>
  <strong>201410</strong> (for previous period)</p>
  
  <p>Is there a way I can pass this date 2014/11/19 e.g. back to my controller where I can do the manipulation and return 201411 and 201410 back to my dropdown list? Or am I thinking about this the wrong way? I have read plenty of Javascript articles but none do exactly what I need to do. How and what is the best way to go about this?</p>
  
  <p>I am an MVC and Javascript newby, so struggeling to get my head around this one..</p>
  
  <p><strong>Edit - Here is my code:</strong></p>
  
  <p><strong>Finance.cshtml</strong>:</p>
  
  <pre><code>@model List&lt;FinanceMVC.Models.FinanceViewModel&gt;
  @{
      ViewBag.Title = "Index";
  }
  
  @using (Html.BeginForm("Finance", "Finance", FormMethod.Post, new { @class = "form-horizontal" }))
  {
      &lt;div class="col-md-1 fade in" style="margin-left: 5%; width: 100%; font-size: 11px; height: auto;"&gt;
          &lt;div id="div_Finance_Container" style="width: 100%; height: 80%; overflow: auto; text-align: left;" runat="server" class="scroll-pane"&gt;
              &lt;div style="height: 500px; overflow: auto;"&gt;
                  &lt;div&gt;&lt;a href="#" id="addNew" class="add-another-cat smallest"&gt;Add New&lt;/a&gt;&lt;/div&gt;
                  &lt;table id="dataTable" border="0"&gt;
                      &lt;tr&gt;
                          &lt;th&gt;Invoice Type&lt;/th&gt;
                          &lt;th&gt;Invoice Number&lt;/th&gt;
                          &lt;th&gt;Invoice Number Period&lt;/th&gt;
                          &lt;th&gt;Invoice Date Created&lt;/th&gt;
                          &lt;th&gt;&lt;/th&gt;
                      &lt;/tr&gt;
                      @if (Model != null &amp;&amp; Model.Count &gt; 0)
                      {
                          int j = 0;
                          foreach (var i in Model)
                          {
                          &lt;tr&gt;
                              &lt;td&gt;
                                  &lt;div class="col-md-1" style="width: 20%;"&gt;
                                      &lt;select name="ddlInvoiceType"&gt;
                                          @foreach (var item in Model)
                                          {
                                              foreach(var bItem in item.InvoiceType)
                                              {
                                                  &lt;option value="@bItem.Description"&gt;@bItem.Description&lt;/option&gt;
                                              }
                                          }
                                      &lt;/select&gt;
                                  &lt;/div&gt;
                              &lt;/td&gt;
                              &lt;td&gt;@Html.TextBoxFor(a =&gt; a[j].InvoiceNumber)&lt;/td&gt;
                              &lt;td&gt;@Html.TextBoxFor(a =&gt; a[j].InvoiceNumberPeriod)&lt;/td&gt;
                              &lt;td&gt;@Html.EditorFor(a =&gt; a[j].InvoiceDateCreated)&lt;/td&gt;
                              &lt;td&gt;
                                  @if (j &gt; 0)
                                  {
                                      &lt;a href="#" class="remove"&gt;Remove&lt;/a&gt;
                                  }       
                              &lt;/td&gt;
                          &lt;/tr&gt;
                                  j++;
                          }
                      }
                  &lt;/table&gt;
                  &lt;input type="submit" value="Save Bulk Data" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  } 
  
  
  @section Scripts{
  @Scripts.Render("~/bundles/jqueryval")
  
  &lt;script language="javascript"&gt;
      $(function () {
          $(document).on('change', '#InvoiceDateCreated', function () {
              alert($(this).val());
          });
      });
  &lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $('#[0].InvoiceDateCreated').change(function () {
              $('#[0].InvoiceDateCreated').text('sam');
          });
      });
  &lt;/script&gt;
  
  &lt;script language="javascript"&gt;
      $(document).ready(function () {
  
          //1. Add new row
          $("#addNew").click(function (e) {
              e.preventDefault();
              var $tableBody = $("#dataTable");
              var $trLast = $tableBody.find("tr:last");
              var $trNew = $trLast.clone();
  
              var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
              $trNew.find("td:last").html('&lt;a href="#" class="remove"&gt;Remove&lt;/a&gt;');
              $.each($trNew.find(':input'), function (i, val) {
                  // Replaced Name
                  var oldN = $(this).attr('name');
                  var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
                  $(this).attr('name', newN);
                  //Replaced value
                  var type = $(this).attr('type');
                  if (type.toLowerCase() == "text") {
                      $(this).attr('value', '');
                  }
  
                  // If you have another Type then replace with default value
                  $(this).removeClass("input-validation-error");
              });
              $trLast.after($trNew);
  
              // Re-assign Validation 
              var form = $("form")
                  .removeData("validator")
                  .removeData("unobtrusiveValidation");
              $.validator.unobtrusive.parse(form);
          });
  
          // 2. Remove 
          $('a.remove').live("click", function (e) {
              e.preventDefault();
              $(this).parent().parent().remove();
          });
  
      });
  &lt;/script&gt;
  }
  </code></pre>
  
  <p><strong>Finance.cs</strong> (Model):</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.ComponentModel.DataAnnotations;
  
  namespace FinanceMVC.Models
  {
      //public class Finance
      //{
  
      //}
  
      public partial class FinanceViewModel
      {
          /*ID*/
          [Required]
          [Display(Name = "ID")]
          public int ID { get; set; }
  
          /*BINL_BIND_ID*/
          [Required]
          [Display(Name = "Invoice Type")]
          //public int InvoiceType { get; set; }
          public List&lt;C_InvoiceType&gt; InvoiceType { get; set; }
  
          /*BINL_Inv_Num_Pointer*/
          [Required]
          [Display(Name = "Invoice Number")]
          public char InvoiceNumber { get; set; }
  
          /*BINL_Inv_Num_Period*/
          [Required]
          [Display(Name = "Invoice Number Period")]
          public int InvoiceNumberPeriod { get; set; }
  
          /*BINL_Created*/
          [Display(Name = "Invoice Created Date")]
          [Required]
          [DataType(DataType.DateTime)]
          public DateTime InvoiceDateCreated { get; set; }
  
          /*BINL_SystemInserted*/
          [Required]
          [Display(Name = "Invoice System Inserted")]
          public bool InvoiceSystemInserted { get; set; }
  
  
      }
  
      public class C_InvoiceType
      {
          public int ID { get; set; }
          public string Description { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>FinanceController.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Mvc;
  using FinanceMVC.Models;
  using System.Threading.Tasks;
  using System.Data;
  using System.Data.SqlClient;
  
  namespace FinanceMVC.Controllers
  {
      public class FinanceController : Controller
      {
          public ActionResult Finance()
          {
              FinanceViewModel FVM = new FinanceViewModel();
  
              // This is only for show by default one row for insert data to the database
              List&lt;FinanceViewModel&gt; FinanceViewList = new List&lt;FinanceViewModel&gt; 
              { 
                  new FinanceViewModel 
                  { 
                      ID = 0 , InvoiceType = ReturnInvoiceType(), InvoiceNumber = '\0', InvoiceNumberPeriod = 0, InvoiceDateCreated = DateTime.Now, InvoiceSystemInserted = false 
                  } 
              };
  
              return View(FinanceViewList);
          }
  
          // 
          // GET: /Finance/ 
  
          /*Matches file name of Finance.cshtml*/
          [HttpPost]
          public ActionResult Finance(List&lt;FinanceViewModel&gt; model)
          {
              if (ModelState.IsValid)
              {
  
              }
  
              return View(model);
          }
  
          // 
          // GET: /Finance/Welcome/ 
  
          public string Welcome()
          {
              return "This is the Welcome action method...";
          }
  
          public static List&lt;C_InvoiceType&gt; ReturnInvoiceType()
          {
              string sQ = ""
                         + " Select ID, BIND_Description \n"
                         + " From Biller_InvoiceNum_DefSet with(nolock) \n"
                         ;
              try
              {
                  using (SqlConnection sCon = new SqlConnection(Application_Info.sMOB_Master_Conn()))
                  {
                      if (sCon.State != ConnectionState.Open)
                      {
                          sCon.Open();
                      }
  
                      using (SqlCommand sCmd = new SqlCommand(sQ, sCon))
                      {
                          using (SqlDataReader sRdr = sCmd.ExecuteReader())
                          {
                              List&lt;C_InvoiceType&gt; list_InvoiceType = new List&lt;C_InvoiceType&gt;();
                              while (sRdr.Read())
                              {
                                  list_InvoiceType.Add(new C_InvoiceType
                                  {
                                      ID = (int)sRdr["ID"],
                                      Description = (string)sRdr["BIND_Description"]
                                  });
                              }
                              return list_InvoiceType;
                          }
                      }
                  }
              }
              catch
              {
  
              }
  
              return ReturnInvoiceType();
          }
      }
  }
  </code></pre>