0

Как вызвать метод контроллера Assortiment с помощью скрипта и передать в него значение Category. При нажатии на кнопку вылезает ошибка

Uncaught ReferenceError: $ is not defined
sort_submit @ Assortiment?Category=all:43
onclick @ Assortiment?Category=all:40   

cshtml:

<input id="Category" onfocus="if (this.value == '@ViewBag.Category') {this.value = ''; this.style.color = '#000';}"
    onblur="if (this.value == '') {this.value = '@ViewBag.Category'; this.style.color = '#777';}"
    type="text" [email protected] />
<input onclick="sort_submit(Category)" type="button" value="Sort" />
<script>
    function sort_submit(Category) {
        $.ajax({
            url: '/Admin/Assortiment',
            type: 'POST',
            data: { Category: Category },
            success: null
        })
    }
</script>

Controller:

public ActionResult Assortiment(String Category)
{
    if (Category == "all" || Category == null)
    {
        Item[] items = DBProvider.getAllItems();
        ViewBag.items = items;
        ViewBag.Category = "Category";
        return View();
    }
    else
    {
        Item[] items = DBProvider.getItemsByCategory(Category);
        ViewBag.items = items;
        ViewBag.Category = Category;
        return View();
    }
}

upd:

Uncaught TypeError: Illegal invocation
e @ jquery-1.11.3.min.js:5
Vb @ jquery-1.11.3.min.js:5
Vb @ jquery-1.11.3.min.js:5
Vb @ jquery-1.11.3.min.js:5
m.param @ jquery-1.11.3.min.js:5
m.extend.ajax @ jquery-1.11.3.min.js:5
sort_submit @ Assortiment?Category=all:44
onclick @ Assortiment?Category=all:41

44 строка : $.ajax({

16
  • добавьте <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>в секцию head и обработчик success в $.ajax
    – user176262
    7 дек 2015 в 16:36
  • @Igor теперь такая ошибка Uncaught TypeError: Illegal invocation, как правильно тип задать?
    – max619
    7 дек 2015 в 16:52
  • что такое Category в onclick="sort_submit(Category)"?
    – user176262
    7 дек 2015 в 16:59
  • Поменяйте data: { String: Category }, на data: { Category: Category },
    – user176262
    7 дек 2015 в 17:05
  • @Igor та же самая ошибка
    – max619
    7 дек 2015 в 17:08

1 ответ 1

0

Если Category переменная то:

<input onclick="sort_submit('@Category')" type="button" value="Sort" />

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.