I have an MVC C# Web Application. In a modal, I am requesting the user to enter a few fields about their company. Several of the inputs are dropdowns populated by a database. My issue is that the dropdown fields are not required, and the page is not valid unless there is a selected value. How can I make this optional? The specific error that I get regarding the "State" dropdown is: "Key:State_ID Error". If I make a selection in the "State" dropdown, page is valid and data is inserted into the database.
Controller Snippet -------------------
[HttpGet]
public ActionResult Client()
{
ViewData["Success"] = (string)TempData["Success"] ?? "";
VM_Client vmClient = new VM_Client
{
Clients = clientDao.GetClients()
};
GetUserCookies();
return View("~/Views/Maintenance/Master/Client.cshtml", vmClient);
}
[HttpGet]
public ActionResult AddClient()
{
VM_AddClient addClient = new VM_AddClient
{
States = stateDao.GetStates(),
Countries = countryDao.GetCountries(),
TimeZones = timeZoneDao.GetTimeZones(),
Industries = industryDao.GetIndustries(),
CompanyTypes = companyTypeDao.GetCompanyTypes()
};
return PartialView("~/Views/Shared/Modals/Client/_AddClientPartial.cshtml", addClient);
}
[HttpPost]
[ActionName("AddClient")]
public ActionResult AddClientPost(Client client)
{
if (ModelState.IsValid)
{
client.Created_By = Request.Cookies.Get("User_ID").Value;
client.Updated_By = Request.Cookies.Get("User_ID").Value;
try
{
clientDao.InsertClient(client);
TempData["Success"] = "true";
}
catch (Exception e)
{
TempData["Success"] = "false";
}
}
var errors2 = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
TempData["Success"] = "false";
return RedirectToAction("Client", "Maintenance");
}
View Snippet -------------------------
<div class="row mg-t-20">
<label class="col-sm-5 form-control-label">State: </label>
<div class="col-sm-6 mg-t-10 mg-sm-t-0">
<select name="State_ID" id="add_State_ID" class="form-control">
<option value="">Select a State</option>
@{
foreach (State state in Model.States)
{
<option value="@state.State_ID">(@state.State_Abr) @state.State_Name</option>
}
}
</select>
</div>
<label class="col-sm-1 form-control-label"><span class="tx-danger"></span></label>
</div>
Dapper Snippet -------------------------
public List<Client> GetClients()
{
List<Client> allClients = new List<Client>();
string sqlQuery = "EXEC dbo.[getClients]";
allClients = (List<Client>)Query<Client>(sqlQuery);
return allClients;
}
public void InsertClient(Client client)
{
string sqlQuery = @"EXEC dbo.[insClient]
@Client_Number = '',
@Client_Initials = @Client_Initials,
@Client_Name = @Client_Name,
@Address_1 = @Address_1,
@Address_2 = @Address_2,
@City = @City,
@State_ID = @State_ID,
@Zip = @Zip,
@Zip4 = @Zip4,
@Country_ID = @Country_ID,
@Phone = @Phone,
@Fax = @Fax,
@Web_Address = @Web_Address,
@Active = @Active,
@Company_Type_ID = @Company_Type_ID,
@Time_Zone_ID = @Time_Zone_ID,
@Industry_ID = @Industry_ID,
@Created_By = @Created_By,
@Updated_By = @Updated_By";
Execute(sqlQuery, client);
}
Model Snippet -------------------------
public class Client
{
public string Client_ID { get; set; }
public string Client_Number { get; set; }
public string Client_Initials { get; set; }
public string Client_Name { get; set; }
public string Address_1 { get; set; }
public string Address_2 { get; set; }
public string City { get; set; }
public int State_ID { get; set; }
public string State_Name { get; set; }
public string State_Abr { get; set; }
public string Zip { get; set; }
public string Zip4 { get; set; }
public int Country_ID { get; set; }
public string Country_Name { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Web_Address { get; set; }
public bool Active { get; set; }
public int Company_Type_ID { get; set; }
public string Company_Type_Name { get; set; }
public int Time_Zone_ID { get; set; }
public string Time_Zone_Description { get; set; }
public int Industry_ID { get; set; }
public string Industry_Name { get; set; }
public DateTime Created { get; set; }
public string Created_By { get; set; }
public string Created_By_Name { get; set; }
public DateTime Updated { get; set; }
public string Updated_By { get; set; }
public string Updated_By_Name { get; set; }
}
}