AlexGH AlexGH - 4 months ago 32x
Ajax Question

Passing parameter in TextBox using Ajax and MVC

I want in the UI that the user enter a number inside a textbox and then show him in the same page using AJAX a record of all the artists with age less than the number the user entered.
My doubt is that I don't know how to pass the number entered into the textbox using Ajax to the PartialViewResult method inside my Controller Class

Controller class:

public PartialViewResult GetByAgeLessThan(int age)
List<Artist> list = ope.ArtistLessThanAge(age);
return PartialView("GetByAgeLessThan", list);

This is my Artists index view(I want to show the results in it using Ajax). My big doubt is in here.. what should I do to pass the number entered inside the textbox to my PartialViewResult inside my controller??? There should be something wrong in this code... when I press the submit button nothing happens:

@using (Ajax.BeginForm("GetByAgeLessThan", "Artists", new AjaxOptions()
HttpMethod = "GET",
UpdateTargetId = "divajax",
InsertionMode = InsertionMode.Replace
<input type="submit" name="Command" value="Search"/>


When razor executes the line @Html.TextBox("txtAge"), It is going to render the below output

<input id="txtAge" name="txtAge" type="text" value="">

For model binding to work, you need to have your form field names same as your action method parameter name(s).

So change




and it should work fine.

Or you can update your action method parameter name to txtAge ( I don't like that parameter name :) )