1

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>

1 Answer 1

2

The problem here is you are setting the parameter in ajax url from razor server side variable, so after modifying the values in textboxes, they would'nt get updated in the javascript call, as the razor variable will get evaluated at view render time, so you need to read the values just before sending the ajax call via jquery.

you need to change following :

url: 'Person/' + '@Model.Name' + '/' + @Model.SocialSecurityNumber,

to:

url: 'Person/' + $("#Name").val() + '/' + $("#SocialSecurityNumber").val(),

The 0 was getting posted because for int the default value in 0, so it got posted but for string nothing was posted in the url.

Sign up to request clarification or add additional context in comments.

7 Comments

I still get error but another one, url is: localhost:51446/Person/ Error: SyntaxError: Unexpected token < in JSON at position 0
is your ajax call reaching controller action now?
the above line should work, make sure you don't have any other syntax errors in your code
I´ve already checked. Since it works with hardcoded url it seems to be something else, the value of val() returns "" :/
try setting id of editors yourself and use that in jquery selector like : @Html.EditorFor(model => model.Name, new { htmlAttributes = new { placeholder = "Enter name" id="txtBoxName" } }) and in ajax call $("#txtBoxName").val() as you have labels as well, so might be they are conflicting
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.