locked
adding parameters to an action link for a controller action RRS feed

  • Question

  • User-494985264 posted

    I am trying to send parameters from an actionlink to an controller. It says input string was not in the correct format.

    Controller

    public ActionResult Select(string name, string course, int scheduled_time)
    {
    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "localhost";
    conn_string.UserID = "root";
    conn_string.Password = "pluto7788aa";
    conn_string.Database = "smartform4";

    using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
    using (MySqlCommand cmd = conn.CreateCommand())
    { //watch out for this SQL injection vulnerability below


    cmd.CommandText = string.Format("INSERT INTO tips VALUES({name}, {course}, {scheduled_time})");

    conn.Open();
    cmd.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
    //return View(db.tips.ToList());
    }

    view

    <td>
    @Html.ActionLink("Publish", "Publish", new { Id=item.id }, null) |
    @Html.ActionLink("Select", "Select", new { name =item.name,course = item.course, scheduled_time = item.scheduled_time }) |
    </td>

    Model

    public class SelectTip
    {
    //[Key]
    public int id { get; set; }
    public string course { get; set; }//from daily races

    public DateTime scheduled_time { get; set; }//from daily races

    public string name { get; set; }//from daily_runners
    }

    Basically i am trying to copy a row from one table and insert it into another 

    Friday, March 9, 2018 2:53 PM

All replies

  • User475983607 posted

    scheduled_time is defined as an int in the action method...

    public ActionResult Select(string name, string course, int scheduled_time)
    {

    but declared as a DateTime in the class.

    public DateTime scheduled_time { get; set; }//from daily races

    Friday, March 9, 2018 3:25 PM
  • User-494985264 posted

    My controller is now

    Controller

    public ActionResult Select(string name, string course, DateTime scheduled_time)
    {
    MySqlConnectionStringBuilder conn_string = new MySqlConnectionStringBuilder();
    conn_string.Server = "localhost";
    conn_string.UserID = "root";
    conn_string.Password = "pluto7788aa";
    conn_string.Database = "smartform4";

    using (MySqlConnection conn = new MySqlConnection(conn_string.ToString()))
    using (MySqlCommand cmd = conn.CreateCommand())
    { //watch out for this SQL injection vulnerability below
    //cmd.CommandText = string.Format("INSERT Test (lat, long) VALUES ({0},{1})",
    // OSGconv.deciLat, OSGconv.deciLon);

    cmd.CommandText = string.Format("INSERT INTO tips VALUES({name}, {course}, {scheduled_time})");

    conn.Open();
    cmd.ExecuteNonQuery();
    }

    return RedirectToAction("Index");
    //return View(db.tips.ToList());
    }

    Error

    The parameters dictionary contains a null entry for parameter 'scheduled_time' of non-nullable type 'System.DateTime' for method 'System.Web.Mvc.ActionResult Select(System.String, System.String, System.DateTime)' in 'HorseTrak10.Controllers.SelectTipController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
    Parameter name: parameters]
       
    

    Friday, March 9, 2018 3:41 PM
  • User475983607 posted

    The error is very clear.  You are passing null to the action method but the action method and model are not expecting a null.  I'm not sure how you application works but if null is allowed then use the nullable operator.

    public DateTime? scheduled_time { get; set; }//from daily races
    public ActionResult Select(string name, string course, DateTime? scheduled_time)

    Aside from that, the action method has errors.  So once you get past the model binding, I believe the  INSERT will fail because it is missing the quotes and I'm not sure if the date format is going to work.

    I strongly suggest that you stop what you're doing and go through the Getting Started tutorials.

    Friday, March 9, 2018 4:01 PM