0
votes

I have trying to pass a query string parameter to my JsonResult action in the controller. I keep getting the following error:

Value cannot be null. Parameter name: String

I need the task_id from this url:

TaskIn?task_id=33

In my view I have tried (fails with same error):

 @model TaskingSystem.Models.AcceptTasksViewModel

 @{string task_id = @Request.QueryString["task_id"];}
 @Html.HiddenFor(m => m.task_id)

In controller:

  public JsonResult TasksExist(string email, string task_id)
    {

        int tasks_id = int.Parse("task_id");
        return Json(db.Tasks_Validate.Any(e => e.Email == email && e.task_id == tasks_id), JsonRequestBehavior.AllowGet);

    }

My model:

 public class AcceptTasksViewModel{

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email:")]
    //Using Remote validation attribute   
    [Remote("TasksExist", "Task_Results",  ErrorMessage = "Email does not exists in database. Please try a different email address.")]
    public string email { get; set; }

    public int task_id { get; set; } 

    public string FirstName { get; set; }

    public string LastName { get; set; }
}

I have also tried just passing straight into the action using this but it still fails.

  int tasks_id = int.Parse(Request.Params["task_id"]);
1
Why not change to the type to int here: public JsonResult TasksExist(string email, string task_id)Mark Schultheiss

1 Answers

0
votes

To pass the task_id along with the email to your TasksExist(string email, string task_id) method, you have to mention task_id in AdditionalFields property of Remote attribute as follows:

[Remote("TasksExist", "Task_Results",AdditionalFields = "task_id"  ErrorMessage = "Email does not exists in database. Please try a different email address.")]
public string email { get; set; }