0

I have my textboxfor field which select the data from role table from the database i need this textboxfor field make a autocomplete with starting character.

Sample:

MVC code:

This JsonResult in the UsersControl

 public JsonResult GetRoles(string Prefix)
    {
        var RoleListVal = db.Roles.Where(r => r.Deleted == false)
            .Where(r => r.Name.ToUpper().StartsWith(Prefix))
            .Select(r => new { Name = r.Name, ID = r.RoleID }).Distinct().ToList();
        return Json(RoleListVal, JsonRequestBehavior.AllowGet);
    }

cshtml Code:

This the JS and the HTML5 TextBoxFor:

$("#keywords-manual").autocomplete({
        source: "/Users/GetRoles",
        minLength: 1
    })
<div class="form-inline">
            @Html.Label("Role :")
            @Html.TextBoxFor(model => model.Roles.FirstOrDefault().Name, new { @class = "form-control", @id = "keywords-manual" })
</div>

I don't why isn't working!

8
  • Try to return an array instead of json Commented Sep 28, 2016 at 11:03
  • why isn't working! is off topic Commented Sep 28, 2016 at 11:09
  • Akshay -not understand you Commented Sep 28, 2016 at 11:11
  • Div - i don't know why Commented Sep 28, 2016 at 11:11
  • Your server side method is hitting or not when you type in autocomplete? If it is hitting then RoleListVal is filled with values ? Commented Sep 28, 2016 at 11:14

2 Answers 2

2

Here your controller side method looks good.

But issue is while you calling method.

    $("#keywords-manual").autocomplete({
        source: function (request, response) {
            $.ajax(
                    {
                        type: "POST",
                        url: '@Url.Action("GetRoles","Users")',
                        data: { Prefix: $('#keywords-manual').val() },
                        dataType: "json",
                        success: function (data) {
                            response($.map(data, function (item) {
                                return {
                                    value: item.Name,
                                    description: item
                                }
                            }))
                        },
                        error: function (error) {
                            console.log(error);
                        }
                    });
        },
        minLength: 1,
        select: function (event, ui) {

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

5 Comments

Not make anything also
Not hit the method
Is it must add any reference to the html page ?
the link attributes depend on what ?
Use [HttpPost] for hitting the method
-1

This is how you configure the Autocomplete plug in

On HTML Side:

$(function () {
            $("#keywords-manual").autocomplete({
                source: "@Url.Action("GetRoles","Users")",
                minLength: 1
            })
                   });

On the controller side:

public JsonResult GetRoles(string term)
    {
        var RoleListVal = db.Roles.Where(r => r.Deleted == false)
            .Where(r => r.Name.ToUpper().StartsWith(term))
            .Select(r => new { label = r.Name, id = r.RoleID,value=r.RoleID }).Distinct().ToList();
        return Json(RoleListVal, JsonRequestBehavior.AllowGet);
    }

3 Comments

@Div the autocomplete plugin does that automatically
@AhmedHossamAlhansy It may have a syntax error, but you should be able to fix it, if not, just put the error message and I will let you know how
@AhmedHossamAlhansy Open the dev toolbar, then type something in the textbox, check the network tab and see if it fires a request to the backend or not and inspect the requested URL and query string parameter, check also if the backend method in the controller is called or not and if it is called, check the results and also make sure the console tab in the dev toolbar is not showing any errors, if it shows any error, please copy it here

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.