locked
How to get a SelectedValue @Html.DropDownList and INSERT into Database in Razor C# RRS feed

  • Question

  • User1007306150 posted

    I have a dropdownlist, which I have filled with items from a database. The dropdownlist displays the item name however the value when selected is the ID of the item and not the name.

    I want to insert this a new record on a different table but I am getting the following error and I have attempted to research the problem:

    Compiler Error Message: CS0103: The name 'customerID' does not exist in the current context

    @{
    Layout = "~/_Layout.cshtml";
    Page.Title = "Add Server";
    
    var db = Database.Open("BackupMonitoringAppV2") ;
    var listcustomers = "SELECT CustomerName, CustomerID from Customers";
    
    List<SelectListItem> customerdropdowndata = new List<SelectListItem>();
    bool isSelected = false;
    foreach(var item in db.Query(listcustomers)){   
        customerdropdowndata.Add(new SelectListItem
        {
    
            Text = item.CustomerName,
            Value = item.CustomerID.ToString(), 
            Selected = isSelected
        });
    }
    
                ///Need to resolve the issue with request form adding to database.
    
     var servername = "";
    
    
     if(IsPost){
        customerID = Request.Form["Customers"];
        servername = Request.Form["ServerName"];
    
        var db2 = Database.Open("BackupMonitoringAppV2");
        var insertCommand = "INSERT INTO Servers (CustomersID, Servername,) Values(@0, @1)";
        db.Execute(insertCommand, customerID, servername);
        Response.Redirect("~/Dashboard/Servers");
    
    }
    <form role="form" method="post">
      <div class="form-group">
        
          @Html.DropDownList("Customers", customerdropdowndata)
      </div>
      
        <div class="form-group">
        <label for="CustomerName">Server Name</label>
          
        <input type="text" class="form-control" name="customername" value="@Request.Form["ServerName"]" />
      </div>
      
      <button type="submit" class="btn btn-info" name="buttonSubmit">Add Server</button>
    </form>



    Wednesday, July 6, 2016 10:48 AM

Answers

  • User379720387 posted

    Note:

    how the select list is created

    how the helper is pieced together

    and what is requested.

    Use the debugger to see what is returned by the Request

    var locdata = db.Query("the query here");
        var locs = locdata.Select(item => new SelectListItem
        {
            Value = item.locationId.ToString(),
            Text = item.Location
        });
    
    @Html.DropDownList("locationId", "-- Select --", locs)
    
    var locationid = Request["locationId"].AsInt();



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 6, 2016 11:42 AM

All replies

  • User379720387 posted

    Note:

    how the select list is created

    how the helper is pieced together

    and what is requested.

    Use the debugger to see what is returned by the Request

    var locdata = db.Query("the query here");
        var locs = locdata.Select(item => new SelectListItem
        {
            Value = item.locationId.ToString(),
            Text = item.Location
        });
    
    @Html.DropDownList("locationId", "-- Select --", locs)
    
    var locationid = Request["locationId"].AsInt();



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 6, 2016 11:42 AM
  • User1007306150 posted

    Note:

    how the select list is created

    how the helper is pieced together

    and what is requested.

    Use the debugger to see what is returned by the Request

    var locdata = db.Query("the query here");
        var locs = locdata.Select(item => new SelectListItem
        {
            Value = item.locationId.ToString(),
            Text = item.Location
        });
    
    @Html.DropDownList("locationId", "-- Select --", locs)
    
    var locationid = Request["locationId"].AsInt();


    Hi wavemaster, thank you for your prompt reply!

    I have managed to populate the dropdownlist with the "CustomerNames" from the database, however I am now stuck at submitting the value of the field along with another text field to the server. 

    When trying to code the process of  inserting the requested fields into the database I get the following error:

    1 :  - Input string was not in a correct format.

    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.FormatException: 1 :  - Input string was not in a correct format.

    Source Error: 

    Line 21:         var db2 = Database.Open("BackupMonitoringAppV2") ;
    Line 22:         var insertCommand = "INSERT INTO Servers (ServerName, CustomerID) Values(@0, @1)";
    Line 23:         db.Execute(insertCommand, servername, customerid);
    Line 24: 
    Line 25: 
    Wednesday, July 6, 2016 6:22 PM
  • User379720387 posted

    What is the date type of the CustomerID column, and did you do a type conversion on the request?

    Wednesday, July 6, 2016 6:31 PM
  • User1007306150 posted

    I was able to get this working after reading through the code several times I finally understood the concept. 

    Thank you so much for your help, I really appreciate it!

    Wednesday, July 6, 2016 7:00 PM