JQuery Autocomplete没有将值传递给MVC控制器

时间:2013-08-04 11:38:20

标签: jquery asp.net-mvc jquery-ui asp.net-mvc-4

我正在尝试从使用jquery Autocomplete填充的文本框中传递所选项目的值。这就是我到目前为止所做的:

<script type="text/javascript">

        $(function () {
            var userId = $('#userID').val();
            //http://techbrij.com/987/jquery-ui-autocomplete-asp-net-web-api
            $("#autocomplete").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "/api/Friends/" + userId,
                        type: 'GET',
                        cache: false,
                        data: request,
                        dataType: 'json',
                        success: function (json) {
                            // call autocomplete callback method with results 
                            response($.map(json, function (name) {
                                return {
                                    label: name.FullName,
                                    value: name.FriendID
                                };
                            }));
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            $("#autocomplete").text(textStatus);
                        }
                    });
                },
                select: function (event, ui) {
                    alert('you have selected ' + ui.item.label + ' ID: ' + ui.item.value);
                    $('#autocomplete').val(ui.item.label);
                    return false;
                },
                messages: {
                    noResults: '',
                    results: function () {
                    }
                }
            });
        });

    </script>

查看

@using (Html.BeginForm()) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>MailMessage</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Recipient.ID)
        </div>
        <div class="editor-field">
            <input type="text" id="autocomplete" />
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Subject)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Subject)
            @Html.ValidationMessageFor(model => model.Subject)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Message)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Message)
            @Html.ValidationMessageFor(model => model.Message)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

控制器

[HttpPost]
public ActionResult Compose(MailMessage mailMessage)
{
    mailMessage.MailDate = DateTime.Now;
    mailMessage.Recipient = UserManager.GetFullUser(mailMessage.Recipient.ID);
    mailMessage.Sender = SDSession.SDUser;
    mailMessage.MessageId = Guid.NewGuid();

    MailMessageManager.AddMailMessage(mailMessage);
    return View("Inbox");
}

这是一个减少版本,但它说明了这个问题。它在从api获取数据时工作正常,我选择了所需的用户,然后点击提交按钮,但在控制器中它需要一个MailMessage对象,所以没有为我所拥有的用户传递任何内容在自动填充中选择,我应该在文本框中添加名称属性,还是有更好的方法来执行此操作?

1 个答案:

答案 0 :(得分:3)

尝试使用以下行替换输入标记的自动完成ID。

 @Html.TextBoxFor(model => model.Recipient.ID, new { @id="autocomplete" })

这可以解决您的问题。