title: How to save data into DataTable in MVC3?
body: <p>I have mvc3 application in this i have used two partial views 1.controls 2.webgrid</p>
  
  <p>inside controls i'm populating dropdownlists from actual database tables. using EF</p>
  
  <p>On index.cshtml i have one form in which need to select values from these dropdown lists and when press insert button these values should have to go to Temp "DataTable" and also show it in webgrid...I'm newbie to MVC3 and dont know how to do this.</p>
  
  <pre><code>Controls.cshtml
  @model Mapping.Models.SecurityIdentifierMappingViewModel
  @using (Html.BeginForm())
  {
      @Html.ValidationSummary(true)
      &lt;fieldset&gt;
          &lt;legend&gt;Mapping&lt;/legend&gt;
          &lt;div class="editor-label"&gt;
              @Html.Label("Pricing SecurityID")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.HiddenFor(model =&gt; model.MappingControls.Id)
              @Html.DropDownListFor(model =&gt; model.MappingControls.PricingSecurityID,
           new SelectList(Model.PricingSecurities, "Value", "Text"),
           "Select SecurityID"
              )
              @Html.ValidationMessageFor(model =&gt; model.MappingControls.PricingSecurityID)
          &lt;/div&gt;
          &lt;div class="editor-label"&gt;
              @Html.Label("CUSIP ID")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.DropDownListFor(model =&gt; model.MappingControls.CUSIP,
           new SelectList(Model.CUSIPs, "Value", "Text"),
              "Select CUSIP"
              )
              @Html.ValidationMessageFor(model =&gt; model.MappingControls.CUSIP)
          &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.Label("Calculation")
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.TextBoxFor(model =&gt; model.MappingControls.Calculation)
              @Html.ValidationMessageFor(model =&gt; model.MappingControls.Calculation)
          &lt;/div&gt;
          &lt;p&gt;
              &lt;input id="btnsubmit" type="submit" value="Insert" /&gt;
  
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  
  HomeController.cs
  public class HomeController : Controller
      {
          //
          // GET: /Home/
  
          mydataEntities dbContext = new mydataEntities();
          DataRepository objRepository = new DataRepository();
  
          //GET
  
          public ActionResult Index(string userAction , int uid = 0)
          {
              var mappingobj = new SecurityIdentifierMappingViewModel();
              mappingobj.MappingWebGridList = dbContext.SecurityIdentifierMappings.ToList();
  
                  mappingobj.MappingControls = new SecurityIdentifierMapping();
                  mappingobj.MappingControls.PricingSecurityID = 0;
                  mappingobj.MappingControls.CUSIP = string.Empty;
  
              mappingobj.PricingSecurities = objRepository.GetPricingSecurityID();
              mappingobj.CUSIPs = objRepository.GetCUSIP();
  
              return View(mappingobj);
          }
  
  
          //POST
  
          [HttpPost]
          public ActionResult Index(SecurityIdentifierMappingViewModel objModel)
          {
  
  
              if (objModel.MappingControls.Id &gt; 0)
              {
                  if (ModelState.IsValid)
                  {
                      dbContext.Entry(objModel.MappingControls).State = EntityState.Modified;
                      try
                      {
                          dbContext.SaveChanges();
                          //objModel = new SecurityIdentifierMappingViewModel();
                          //return RedirectToAction("Index", "Home");
                      }
                      catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                      {
                          throw;
                      }
                  }
  
              }
  
              //insert code
              else
              {
                  if (ModelState.IsValid)
                  {
                      dbContext.SecurityIdentifierMappings.Add(objModel.MappingControls);
                      try
                      {
                          dbContext.SaveChanges();
                      }
                      catch (System.Data.Entity.Validation.DbEntityValidationException ex)
                      {
                          throw;
                      }
                  }
  
              }
  
              return RedirectToAction("Index");
          }
      }
  
  public class SecurityIdentifierMappingViewModel
  {
      public IEnumerable&lt;SecurityIdentifierMapping&gt; MappingWebGridList { get; set; }
      public SecurityIdentifierMapping MappingControls { get; set; }
  
      public List&lt;SelectListItem&gt; PricingSecurities { get; set; }
      public List&lt;SelectListItem&gt; CUSIPs { get; set; }
  }
  </code></pre>
  
  <p>Currently using SecurityIdentifierMapping as a 3rd table from database in which inserting my form data ... but need to insert it into "DataTable" </p>