【问题标题】:How to get the value of DropDownList and pass it into AJAX call in MVC5?如何获取 DropDownList 的值并将其传递给 MVC5 中的 AJAX 调用?
【发布时间】:2014-11-05 07:45:59
【问题描述】:

我有一个 MVC5 应用程序,我有一个这样定义的模型:

public class Request
{
    ...

    [ForeignKey("State")]
    public int StateID { get; set; }

    public virtual State State { get; set; }

    public string ServiceName { get; set; }
}

我的状态模型定义如下:

public class State
{
    public int StateID { get; set; }
    public string StateCode { get; set; }
    public string StateName { get; set; }
}

在我看来,我正在工作,我有这样的事情:

        <div class="form-group">
            @Html.LabelFor(model => model.StateID, "State", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("StateID", null, "Please select a state", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StateID, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ServiceName, "Service", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ServiceName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ServiceName, "", new { @class = "text-danger" })
            </div>
        </div>

重点是我想在我的 ServiceName 输入框中插入自动完成功能,为此我编写了 JsonResult 方法,定义如下:

    public JsonResult GetBusinessDesriptions(int state, string term)
    {
        var results = db.Users.OfType<Business>().Where(b => b.StateID == state && (term == null || b.Description.ToLower().Contains(term.ToLower()))).Select(x => new { id = x.StateID, value = x.Description }).Take(5).ToList();

        return Json(results, JsonRequestBehavior.AllowGet);
    }

然后,我想在我的 JS 中使用 AJAX 调用它,但我不知道如何实现它。简单地说,我想将用户选择的 StateID 传递给 AJAX 调用和对 GetBusinessDescription 方法的调用。

我有这样的东西,但它不起作用,因为我不知道如何传递视图中选择的 StateID,所以它只读取处于选定状态的企业。

$("#Service-Name").autocomplete({
    source: "/Home/GetBusinessDesriptions",
    minLength: 2,
    select: function (event, ui) {
        $("#Service-Name").val(ui.item.value);
        $("#Service-Name").text(ui.item.value);
    }
});

那么,一旦用户在我的视图中选择 AJAX 调用和我的 GetBusinessDescription 方法,我如何才能将 StateID 的值发送到仅过滤处于选定状态的企业?

【问题讨论】:

标签: jquery ajax asp.net-mvc json jquery-ui-autocomplete


【解决方案1】:

例如,在源选项中使用 ajax 并传递额外的参数。这里StateId是状态下拉列表的id。

$("#Service-Name").autocomplete({
    source: function( request, response ) {
        $.ajax({
          url: "/Home/GetBusinessDesriptions",
          dataType: "jsonp",
          data: {
            state:$("#StateID").val(),
            term: request.term
          },
          success: function( data ) {
            response( data );
          }
        });
      },
    minLength: 2,
    select: function (event, ui) {
        $("#Service-Name").val(ui.item.value);
        $("#Service-Name").text(ui.item.value);
    }
});

【讨论】:

  • 这很好用,只是我需要将 dataType 从 jsonp 更改为 json。感谢您的回答。
  • 很高兴看到使用 Jquery UI 自动完成传递除所需“术语”以外的更多参数的实现。 :)
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 2019-09-03
  • 2013-02-22
  • 1970-01-01
  • 2021-09-01
  • 2016-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多