Hi,
I am new to MVC and binding a table column{names} to dropdown, but I am facing issue on passing selected value to new string action method and respective view is displaying missing parameter error. I am attaching my code snippet below. Kindly check and let me know your solution.
Controller
//Binding Dropdown-Working Fine
public ActionResult Dropdownload()
{
EmployeeContext conn = new EmployeeContext();
var selecteditems = conn.Employees.Select(x=>new {id=x.empid,name=x.empname }).ToList();
ViewBag.employeelist = new SelectList(selecteditems, "id", "name");
return View();
}
//String action value are passing but displaying error
public string clickme(int id)
{
EmployeeContext conn = new EmployeeContext();
string selectedname = conn.Employees.Where(x => x.empid == id).Select(x => x.empname).Single();
return "You selected the name " + selectedname;
}
View
<script src="~/Scripts/jquery-1.10.2.js">
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#btnsubmit").click(function () {
var id = $("#dropid").val();
var data=JSON.stringify({'id':id});
$.ajax(
{
url: '/Employee/Clickme/'+id,
type: "POST",
data: data,
success: function () { alert(data);},
contentType: 'application/json'
});
});
});
</script>
<div style="font-family:'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif">
@using (Html.BeginForm("Clickme","Employee",FormMethod.Post))
{
<h2>Dropdown</h2>
@Html.DropDownList("ddllist",ViewBag.employeelist as SelectList, "Select Name",new { id="dropid"})
<input type="submit" value="submit" id="btnsubmit"/>
}
</div>
Error
Server Error in '/' Application.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.String clickme(Int32)' in 'Learning.Controllers.EmployeeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters