In the editors in view i enter both name and 0 as socialsecuritynumber but wrong method in controller is called, that one with only name as parameter and the value of that is "0", that will say the value of socialsecuritynumber editor. What am I doing wrong ?
EDIT: When i check what url that is invoked in chrome developer tools it says: localhost:51446/Person//0
PersonViewModel
public string Name { get; set; }
public int SocialSecurityNumber { get; set; }
PersonController
[HttpGet]
[Route("Person")]
public ActionResult Index()
{
return View();
}
[HttpGet]
[Route("Person/{name}")]
public JsonResult GetProduct(string name)
{
var person = new PersonViewModel { Name = id, SocialSecurityNumber = 12345678 };
return Json(person, JsonRequestBehavior.AllowGet);
}
[HttpGet]
[Route("Person/{name}/{socialSecurityNumber}")]
public JsonResult GetProduct(string name, int socialSecurityNumber)
{
var person = new PersonViewModel { Name = name, SocialSecurityNumber = 12345678 };
return Json(person, JsonRequestBehavior.AllowGet);
}
Razor View
@model Test.PersonViewModel
<div class="row highlight col-md-10 col-md-offset-2" id="SearchPerson">
<div class="col-md-1">@Html.LabelFor(model => model.Name):</div>
<div class="col-md-2">@Html.EditorFor(model => model.Name, new { htmlAttributes = new { placeholder = "Enter name" } })</div>
<div class="col-md-1">@Html.LabelFor(model => model.SocialSecurityNumber):</div>
<div class="col-md-2">@Html.EditorFor(model => model.SocialSecurityNumber, new { htmlAttributes = new { placeholder = "Enter ssn" } })</div>
<button type="button" class="btn btn-default" id="btnSearchPerson" </button>
</div>
<script type="text/javascript">
$(function() {
$('#btnSearchPerson').on('click', function () {
var $this = $(this);
$this.button('loading');
$.ajax({
type: 'GET',
dataType: 'json',
url: 'Person/' + '@Model.Name' + '/' + @Model.SocialSecurityNumber,
success: function (data) {
$('#SearchPerson').html(data);
},
error: function (xhr, ajaxOption, thorwnError) {
console.log("Error")
},
});
});
</script>