Question: Hi I have question about HTML form array and PHP. For example I have name and email but ask 6 times and I want to send this mail. What I should edit to work? thank you!

**HTML:**

```
   <form  method="Post" action="send.php" onSubmit="return validate();">
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?>
        <b>Data <?php echo $i+1 ?>º</b>
          <input type="edit" name="Name[]" size="40">
          <input type="edit" name="email[]" size="40">
          <br>
    <?php } ?>
          <input type="submit" name="Submit" value="send 6 data mail">
   </form>

```

**send.php:**

```
<?php
       require('phpmailer/class.phpmailer.php');
       $name= $_POST['name[]']; 
       $email= $_POST['email[]']; 
       $mail = new PHPMailer();
       ...
       $mail->Body = ' Data<br> '
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?> '
       name: '.$name[$i].' email: '.$email[$i];
       ...
       $mail->Send();
?>

```

should send:

```
Data
name: nameinput1 email: emailinput1
name: nameinput2 email: emailinput2
name: nameinput3 email: emailinput3
name: nameinput4 email: emailinput4
name: nameinput5 email: emailinput5
name: nameinput6 email: emailinput6

```

Answer: 
I personally prefer to avoid dynamic stuff like ViewBag /ViewData as much as possible to transfer data between action methods and views. Let's build a strongly typed Viewmodel.

```
public class CreateCustomerVM
{
   public string MidName{ get; set; }
   [Required]
   public string FirstName{ get; set; }
   public string Surname{ get; set; }
   public List<SelectListItem> MidNames { set;get;}
   public CreateCustomerVM()
   {
     MidNames=new List<SelectListItem>();
   }
}

```

and in your `Create` action method

```
public ActionResult Create()
{
  var vm=new CreateCustomerVM();
  vm.MidNames=GetMidNames();
  return View(vm);
}
private List<SelectListItem> GetMidNames()
{
  return new List<SelectListItem> { 
    new SelectListItem { Value="Mr", Text="Mr"},
    new SelectListItem { Value="Ms", Text="Ms"},
  };
}

```

and in your view, which is strongly typed to our viewmodel

```
@model CreateCustomerVM
@using(Html.Beginform())
{

 <div>
   Mid name : @Html.DropdownListFor(s=>s.MidName,Model.MidNames)
   FirstName : @Html.TextBoxFor(s=>s.FirstName)
   <input type="submit" />
 </div>

}

```

Now when your form is posted, You will get the selected item value in the `MidName` property of the viewmodel.

```
[HttpPost]
public ActionResult Create(CreateCustomerVM customer)
{
  if(ModelState.IsValid)
  {
    //read customer.FirstName , customer.MidName 
    // Map it to the properties of your DB entity object
    // and save it to DB
  }
  //Let's reload the MidNames collection again.
  customer.MidNames=GetMidNames();
  return View(customer);
}

```
populate in viewbag again in post action of create:

```
public ActionResult Create(){
       ViewBag.Names= new SelectList(db.TbName, "ID", "MidName");
       return View();
    }

[HttpPost]
public ActionResult Create(){
       ViewBag.Names= new SelectList(db.TbName, "ID", "MidName");
       return View();
    }

```

or try with helper like this:

```
@Html.DropDownListFor(x => x.ID, (SelectList)ViewBag.Names,
           new Dictionary<string, object>{{"class", "control-label col-md-2"}})

```