-2

我有文本框。悬停时我可以编辑文本框内容。编辑后我可以保存更改,但是这次文本框的值不会改变,只有在刷新之后。如何更改文本框的值?

样本:

<input type="radio" name="SelectedSavedSearches" id="Saved_search_<%:s.Value %>" value="<%:s.Value %>" />
                                <label for="Saved_search_<%:s.Value %>">&nbsp;</label>
                                <input type="text" class="saved-search-name def-text-input" value="<%: s.Text %>"/>
                                <button class="saved-search-rename"><strong>Save</strong></button>

js:

$('.saved-search-rename', container).unbind('click').click(function () {
        var context = $(this);
        var name = context.closest('p').find('.saved-search-name').val();
        if (name != undefined && name.trim() != '') {
            var id = context.closest('p').find('input[name="SelectedSavedSearches"]').val();
            $.ajax({
                url: '/Account/RenameSavedSearch',
                type: 'POST',
                dataType: 'JSON',
                data: { ssId: id, newName: name },
                success: function (response) {
                    if (!response.result) {
                        btDialogs.Error(response.message);
                    }
                }
            });
            return false;
        } else {
            btDialogs.Error('New Name can\'t be empty');
        }
        return false;
    });

行动:

[HttpPost]
    [ProfileVerification]
    public JsonResult RenameSavedSearch(int ssId, string newName)
    {
        if (LoggedUser == null)
            return Json(new {result = false, message = "Not Authenficated"});

        var search = _listingManagementService.GetSavedSearch(ssId);
        search.Name = newName;
        var confirmation = _listingManagementService.SaveSearch(search);

        return
            Json(
                new
                    {
                        result = confirmation.WasSuccessful,
                        message = confirmation.WasSuccessful ? "Success" : confirmation.Message
                    });
    }
4

1 回答 1

0

Jquery/Javascript 是解决方案

  UPDATE:

我已更新您的操作以返回经过验证的 newName

[HttpPost]
[ProfileVerification]
public JsonResult RenameSavedSearch(int ssId, string newName)
{
    if (LoggedUser == null)
        return Json(new {result = false, message = "Not Authenficated"});

    var search = _listingManagementService.GetSavedSearch(ssId);
    search.Name = newName;
    var confirmation = _listingManagementService.SaveSearch(search);

    return
        Json(
            new
                {
                    result = confirmation.WasSuccessful,
                    message = confirmation.WasSuccessful ? "Success" : confirmation.Message,
                    validatedNewName=confirmation.WasSuccessful ?search.Name:""
                });
}

JS:

 $('.saved-search-rename', container).unbind('click').click(function () {
    var context = $(this);
    var nameInput=context.closest('p').find('.saved-search-name');
    var name = nameInput.val();
    if (name != undefined && name.trim() != '') {
        var id = context.closest('p').find('input[name="SelectedSavedSearches"]').val();
        $.ajax({
            url: '/Account/RenameSavedSearch',
            type: 'POST',
            dataType: 'JSON',
            data: { ssId: id, newName: name },
            success: function (response) {
                if (!response.result) {
                    btDialogs.Error(response.message);
                }
                nameInput.val(!response.validatedNewName);
            }
        });
        return false;
    } else {
        btDialogs.Error('New Name can\'t be empty');
    }
    return false;
});

锋利的 ?

于 2013-10-29T08:57:59.537 回答