文本更改后在文本框中设置值

时间:2013-10-29 08:49:09

标签: javascript asp.net-mvc textbox html-editor

我有文本框。在悬停时,我可以编辑文本框内容。编辑后我可以保存更改,但文本框的值在此时间内不会更改,仅在刷新后。 如何更改textBox的值?

样品:

<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
                    });
    }

1 个答案:

答案 0 :(得分: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;
});
夏普?