title: How to make form field " DeviceId" unique and throw error if repeated, ASP.NET MVC and Azure Cosmosdb using C#?
body: <p>I am using an ASP.NET MVC web application written in C#. I create a form to add entries in Azure Cosmosdb collection. I want to create one field &quot;DeviceId&quot; as a unique field. I am struggling with result.</p>
  <p>Controller:</p>
  <pre><code>  [ActionName(&quot;Create&quot;)]
          public async Task&lt;ActionResult&gt; CreateAsync()
          {//DeviceTypeId dropdown
              List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;();
  
              items.Add(new SelectListItem { Text = &quot;ChainZone&quot;, Value = &quot;1&quot; });
              items.Add(new SelectListItem { Text = &quot;Particle&quot;, Value = &quot;2&quot; });
              items.Add(new SelectListItem { Text = &quot;Raspberry Pi&quot;, Value = &quot;3&quot; });
  
              ViewBag.Type = items;
  
              //Device Stats dropdown
              List&lt;SelectListItem&gt; state = new List&lt;SelectListItem&gt;();
  
              state.Add(new SelectListItem { Text = &quot;Non-Active&quot;, Value = &quot;0&quot; });
              state.Add(new SelectListItem { Text = &quot;Active&quot;, Value = &quot;1&quot; });
              state.Add(new SelectListItem { Text = &quot;In Production&quot;, Value = &quot;2&quot; });
              state.Add(new SelectListItem { Text = &quot;In Maintenance&quot;, Value = &quot;3&quot; });
              state.Add(new SelectListItem { Text = &quot;To Be Installed&quot;, Value = &quot;4&quot; });
  
              ViewBag.State = state;
  
              Device deviceModel = new Device();
              deviceModel.DeviceModelCollection = await DocumentDBRepository&lt;Devicemodel&gt;.GetDeviceModelAsync(d =&gt; d.Id != null);
              return View(deviceModel);
          }
  [HttpPost]
          [ActionName(&quot;Create&quot;)]
          [ValidateAntiForgeryToken]
          public async Task&lt;ActionResult&gt; CreateAsync([Bind(Include = &quot;Id, DeviceId,Lat, Long,DeviceTypeId, DeviceModelId,EnvLog,SpeedLimit,ActivationSpeed,UserName,Password,PhoneNo,IMEI,Puk, SimPin,SpeedLog,GroupId,IPaddress,Name,Address,Status,Notes,DataTypeId,BomLocationId,BatInVoltage,SortValue,Sensors,LastContact,DisconnectedEvents&quot;)] Device item)//LastContact.Ping,LastContact.EnLog,LastContact.SpdLog,LastContact.Voltage,CreatedAt
          {
               var org = await DocumentDBRepository&lt;Device&gt;.GetDevicemapAsync(d =&gt; d.DeviceId == item.DeviceId);
              // var cond = 0;
              //if(org == item.DeviceId) { cond = 1; }
  
              if (ModelState.IsValid)
              {
                  if(org == null)
                  {
                      item.CreatedAt = DateTime.Now;
                      // item.LastContact.Ping = DateTime.Now;
                      await DocumentDBRepository&lt;Device&gt;.CreateDeviceAsync(item);
                      ViewBag.SuccessMsg = &quot;Successfully added&quot;;
                      return RedirectToAction(&quot;Index&quot;, &quot;DeviceMap&quot;);
                  }
              }
  
              return View(item);
          }
  </code></pre>
  <p>Model:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using Newtonsoft.Json;
  using System.Linq;
  using System.Web;
  using System.ComponentModel;
  using System.ComponentModel.DataAnnotations.Schema;
  using java.awt;
  using System.ComponentModel.DataAnnotations;
  using ServiceStack.DataAnnotations;
  using Xunit.Sdk;
  
  namespace WebApplication1.Models
  {
      public class Device
      {
          [Unique]
          [JsonProperty(PropertyName = &quot;DeviceId&quot;)]
          public string DeviceId { get; set; }
          [JsonProperty(PropertyName = &quot;id&quot;)]
          public string Id { get; set; }
  
          [JsonProperty(PropertyName = &quot;Long&quot;)]
          public string Long { get; set; }
  
          [DisplayName(&quot;Devicemodel&quot;)]
          [JsonProperty(PropertyName = &quot;DeviceModelId&quot;)]
          public int DeviceModelId { get; set; }
  
         [JsonProperty(PropertyName = &quot;DeviceTypeId&quot;)]
          public int DeviceTypeId { get; set; }
  
          [JsonProperty(PropertyName = &quot;IPaddress&quot;)]
          public string IPaddress { get; set; }
      }
  }
  </code></pre>
  <p>View:</p>
  <pre><code>@using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
  
  &lt;div class=&quot;form-horizontal&quot;&gt;
      &lt;h4&gt;Device&lt;/h4&gt;
      &lt;hr /&gt;
      @Html.ValidationSummary(true, &quot;&quot;, new { @class = &quot;text-danger&quot; })
  
      &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.DeviceId, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
  
              @Html.EditorFor(model =&gt; model.DeviceId, new { htmlAttributes = new { @class = &quot;form-control&quot;, required = &quot;required&quot; } })
              @Html.ValidationMessageFor(model =&gt; model.DeviceId, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.DeviceModelId, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
              @Html.DropDownListFor(model =&gt; model.DeviceModelId,
             new SelectList(Model.DeviceModelCollection, &quot;DeviceModelId&quot;, &quot;DeviceModelCode&quot;), &quot;Select&quot;)
              @Html.ValidationMessageFor(model =&gt; model.DeviceModelId, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.DeviceTypeId, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
  
              @Html.DropDownListFor(model =&gt; model.DeviceTypeId, (List&lt;SelectListItem&gt;)ViewBag.Type, &quot;Select&quot;)
              @Html.ValidationMessageFor(model =&gt; model.DeviceTypeId, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
   &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.Long, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
              @Html.EditorFor(model =&gt; model.Long, new { htmlAttributes = new { @class = &quot;form-control&quot;, required = &quot;required&quot; } })
              @Html.ValidationMessageFor(model =&gt; model.Long, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;div class=&quot;form-group&quot;&gt;
          @Html.LabelFor(model =&gt; model.IPaddress, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
          &lt;div class=&quot;col-md-10&quot;&gt;
              @Html.EditorFor(model =&gt; model.IPaddress, new { htmlAttributes = new { @class = &quot;form-control&quot;, required = &quot;required&quot; } })
              @Html.ValidationMessageFor(model =&gt; model.IPaddress, &quot;&quot;, new { @class = &quot;text-danger&quot; })
          &lt;/div&gt;
      &lt;/div&gt;
    &lt;div class=&quot;form-group&quot;&gt;
          &lt;div class=&quot;col-md-offset-2 col-md-10&quot;&gt;
              &lt;input type=&quot;submit&quot; value=&quot;Create&quot; class=&quot;btn btn-default&quot; /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  }
  </code></pre>
  <p>It's not working, with three dropdown fields. I get following error for those fields if I check DeviceId in against available Ids in Controller.</p>
  <blockquote>
  <p>Value cannot be null.
  Parameter name: items
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
  Exception Details: System.ArgumentNullException: Value cannot be null.
  Parameter name: items</p>
  </blockquote>
  <p>If I remove dropdown fields everything works fine.</p>
  <p>Please help.</p>