0

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
})){
    <h4>Search</h4>
    @Html.TextBox("txtAge")        
    <input type="submit" name="Command" value="Search"/>
    }
1
  • I would create a view model class, with the list and the age, that would be sent to the PartialViewResult. return PartialView("GetByAgeLessThan", new MyPartialViewModel { Age = age, List = list}) Commented Jun 21, 2016 at 16:16

1 Answer 1

1

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

@Html.TextBox("txtAge") 

to

@Html.TextBox("age")

and it should work fine.

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

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

1 Comment

it works!! and 'age' parameter changed to 'txtAge' :)

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.