title: Deserializing current JSON Array to perform edit operation using web api
body: <p>I am using web API to edit values from collection in MongoDB.I have added code to fetch values from mongodb and have called it in another to fetch the api to perform edit functionality.</p>
  
  <p>My id is getting passed to the action and still null refernce exception occur when i fix it deserialize array error occur. I tried to put deserialize the array using code found in net but it didnt work! Why does this error occur? I am new in web api and mongo db !!  Please help!</p>
  
  <p><strong>Contact.cs(Model class)</strong></p>
  
  <pre><code>public class Contact
  {
      [BsonId]
      public string Id { get; set; }
      public string Name { get; set; }
      public string Address { get; set; }
      public string Email { get; set; }
      public string Phone { get; set; }
  }
  </code></pre>
  
  <p><strong>mongodbcontroller(Controller)</strong></p>
  
  <pre><code>        [System.Web.Http.HttpPut]
              public Contact Edit(Contact contact)
              {
                  var contactsList = mongoDatabase.GetCollection("contact");
                  WriteConcernResult result;
                  bool hasError = false;
  
                  string errorMessage = string.Empty;
                  try
                  {
                      if (!string.IsNullOrEmpty(contact.Id))
                      {
                          IMongoQuery query = Query.EQ("Id", contact.Id);
                          IMongoUpdate update = MongoDB.Driver.Builders.Update
                              //.Set("Id",contact.Id)
                              .Set("Name", contact.Name)
                              .Set("Address", contact.Address)
                              .Set("Phone", contact.Phone)
                              .Set("Email", contact.Email);
                          result = contactsList.Update(query, update);
                          contactsList.Save(contact);
  
                          hasError = result.HasLastErrorMessage;
                      }
                  }
                  catch(Exception ex)
                  {
                      errorMessage = ex.ToString();
                  }
  
  
                  if (!hasError)
                  {
                      return contact;
                  }
                  else
                  {
                      throw new HttpResponseException(HttpStatusCode.InternalServerError);
                  }
              }
  </code></pre>
  
  <p><strong>TestController(Consuming the api)</strong></p>
  
  <pre><code>     public ActionResult Edit(string id)
          {
              //Contact contact = new Contact();
              Contact contact = new Contact();
              try
              {
                  using (var client = new HttpClient())
                  {
  
  
                      client.BaseAddress = new Uri("http://localhost:61093/api/MongoDb/edit");
                      //HTTP GET
                      var responseTask = client.GetAsync("?id=" + id);
                      responseTask.Wait();
  
                      var result = responseTask.Result;
  
                      if (result.IsSuccessStatusCode)
                      {
  
                          var readTask = result.Content.ReadAsAsync&lt;Contact&gt;();
                          readTask.Wait();
  
                          contact = readTask.Result;
                      }
                  }
              }
              catch (Exception ex)
              {
  
              }
  
  
              return View(contact);
  
  
          }
  
      [HttpPost]
      public ActionResult Edit(Contact contact)
      {
  
          try
          {
              using (var client = new HttpClient())
              {
  
                  client.BaseAddress = new Uri("http://localhost:61093/api/MongoDb/edit");
                  //HTTP GET
                  var putTask = client.PutAsJsonAsync&lt;Contact&gt;("contact",contact);
                  putTask.Wait();
  
                  var result = putTask.Result;
                  if (result.IsSuccessStatusCode)
                  {
                      return RedirectToAction("Index");
                  }                     
              }
          }
          catch (Exception ex)
          {
  
          }
  
  
          return View(contact);
  
      }
  
  </code></pre>
  
  <p><strong>Edit.cshtml(View)</strong></p>
  
  <pre><code>
      @model TestApi.Models.Contact
  
    @{
        ViewBag.Title = "Edit";
    }
  
    &lt;h2&gt;Edit&lt;/h2&gt;
  
  
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
  
        &lt;div class="form-horizontal"&gt;
            &lt;h4&gt;Contact&lt;/h4&gt;
            &lt;hr /&gt;
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model =&gt; model.Id)
  
            &lt;div class="form-group"&gt;
                @Html.LabelFor(model =&gt; model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                &lt;div class="col-md-10"&gt;
                    @Html.EditorFor(model =&gt; model.Name, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model =&gt; model.Name, "", new { @class = "text-danger" })
                &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="form-group"&gt;
                @Html.LabelFor(model =&gt; model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
                &lt;div class="col-md-10"&gt;
                    @Html.EditorFor(model =&gt; model.Address, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model =&gt; model.Address, "", new { @class = "text-danger" })
                &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="form-group"&gt;
                @Html.LabelFor(model =&gt; model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
                &lt;div class="col-md-10"&gt;
                    @Html.EditorFor(model =&gt; model.Email, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model =&gt; model.Email, "", new { @class = "text-danger" })
                &lt;/div&gt;
            &lt;/div&gt;
  
            &lt;div class="form-group"&gt;
                @Html.LabelFor(model =&gt; model.Phone, htmlAttributes: new { @class = "control-label col-md-2" })
                &lt;div class="col-md-10"&gt;
                    @Html.EditorFor(model =&gt; model.Phone, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model =&gt; model.Phone, "", 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="Save" class="btn btn-default" /&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    }
  
    &lt;div&gt;
        @Html.ActionLink("Back to List", "Index")
    &lt;/div&gt;
  
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
  
  </code></pre>
  
  <p><strong>Screenshots of my code is attched below</strong>
  <a href="https://i.stack.imgur.com/hazvc.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hazvc.png" alt="1"></a>
  <a href="https://i.stack.imgur.com/88t2K.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/88t2K.png" alt="2"></a>
  <a href="https://i.stack.imgur.com/VQow5.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VQow5.png" alt="3"></a>
  <a href="https://i.stack.imgur.com/R760r.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R760r.png" alt="4"></a></p>