简体   繁体   English

仅当数据成功存储到数据库而没有回发asp.net mvc5 EF6时,单击更改按钮

[英]Change Button on-click only if data is successfully stored to database without postback asp.net mvc5 EF6

成功存储数据后,更改按钮无后退

I have a situation wherein I have to change the button "Send Request" to "Request Sent". 我有一种情况,我必须将按钮“发送请求”更改为“请求已发送”。 There are 'n' number of such buttons in my form and i need to change the buttons for all of them 我的表单中有n个这样的按钮,我需要更改所有按钮

The button should change only if data is successfully stored into the database without reloading the form. 仅当数据成功存储到数据库中而不重新加载表单时,该按钮才应更改。 And here is how i have stored data to database. 这就是我将数据存储到数据库的方式。

public ActionResult SendRequest(string RequestId)
{
        MyRequest request = new MyRequest();
        request.UserId = loggedInUserId;
        request.FutureFriendId = RequestId;
        request.Status = RequestStatus.Pending;
        db.MyRequests.Add(request);
        db.SaveChanges();
        return View();
}

Later i might want to change the button "Request Sent" to "Cancel Request" to rollback the operation. 稍后我可能想将按钮“已发送的请求”更改为“取消请求”以回滚操作。 Please give me a solution for the same. 请给我一个解决方案。 Thank you! 谢谢!

I would return a bool from instead the ActionResult. 我会从ActionResult返回布尔值。 Your JS (Jquery) code should look something like this: 您的JS(Jquery)代码应如下所示:

$(btn).click(function(){
$(btn).attr("disabled", "disabled").val("Sending Request...");
$.ajax({
            url: '/yoururl',
            type: 'GET',
            data: { RequestId: '12345' },
            async: true,
            success: function(returnParameters) {
                $(btn).attr("disabled", "disabled").val("Request Sent");
            },
            error: function(jqXHR, textStatus, errorThrown) {
$(btn).removeAttr("disabled").val("Send Request"); // error, reset.
            }
        });
});

This will give three situations to the button: 这将为按钮提供三种情况:

  1. Sending 正在发送
  2. Sent 已发送
  3. Normal 正常

I've also set the button to "disabled" to prevent more clicks incase it is in progress/the request was sent. 我还将按钮设置为“已禁用”,以防止在进行中/发送请求时获得更多点击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM