locked
Message Data Saved RRS feed

  • Question

  • User1979860870 posted

    Hi

      I have below Js file . IN a controller what should be done that if record is saved then Modal Dialog Box should appear "Record Successfully Saved"

    function Add() {
        var objLocation = {
            Id: $('#Id').val().toUpperCase(),
            Description: $('#Description').val().toUpperCase()
        };
        $.ajax({
            url: "/Location/Add",
            data: JSON.stringify(objLocation),
            type: "POST",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (result) {
                loadData();
                $('#myModal').modal('hide');
            },
            error: function (errormessage) {
                alert(errormessage.responseText);
            }
        });
    }
    
    public JsonResult Add(Location objLocation)
            {
                if (ModelState.IsValid)
                {
                    return Json(dbLocation.Add(objLocation), JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { error = "Custom Error Message" });
                }
            }

    Thanks

    Wednesday, May 26, 2021 10:35 AM

All replies

  • User475983607 posted

    I have below Js file . IN a controller what should be done that if record is saved then Modal Dialog Box should appear "Record Successfully Saved"

    It is up to you to as the developer to come up with a standard design if you wish to pass a success message.  I usually return a BadRequest with an error message.  Anyway, you have some design work to do.

    Wednesday, May 26, 2021 10:47 AM
  • User1979860870 posted

    HI mgebhard

      Can u pls guide what changes i needed to do in my above Code in Controller

    Thanks

    Wednesday, May 26, 2021 11:20 AM
  • User475983607 posted

    Can u pls guide what changes i needed to do in my above Code in Controller

    It's difficult to provide assistance.  You are asking how to implement a feature that potentially affects your entire application.  You are the developer and it is your responsibility to define the application requirements.  

    Like I said, I usually return a BadRequest("Messsage").  Detect the error in the AJAX application and write the message to the screen.  For individual field validation, you can validate the form according to the client validation framework you are using.  Take the time to learn the basics rather than posting every little problem you face.

    Wednesday, May 26, 2021 12:08 PM
  • User287926715 posted

    Hi jagjit saini,

    IN a controller what should be done that if record is saved then Modal Dialog Box should appear "Record Successfully Saved"

    You can judge by ajax to pop up information.

    @model WebApplication23.Models.Test
    @{
        ViewBag.Title = "Index";
    }
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <h2>Test Index Page!</h2>
    ID:@Html.TextBoxFor(c => c.Id)
    Description:@Html.TextBoxFor(c => c.Description)
    <button id="save">Save</button>
    
    
    <script>
    
          $('#save').click(function () {
                $.ajax({
                    url: "@Url.Action("index")",
                    data: { Id: $("#Id").val(), Description: $("#Description").val() },
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.Id != null && data.Description != null) {
                            alert("Save success!")
                        } else {
                            alert("Save failed!")
                        }
                    }
    
                    })
            });
    
    
    </script>

    Or you define the content of the pop-up window in the controller, and return the corresponding content through ajax judgment.

    Best Regards,

    ChaoDeng

    Thursday, May 27, 2021 8:15 AM
  • User1979860870 posted

    Hi ChaoDeng

      Thanks but i want message in Modal popup

    Thanks

    Thursday, May 27, 2021 8:40 AM
  • User287926715 posted

    Hi  jagjit saini,

    You can choose what you want according to the method in this document.

    https://getbootstrap.com/docs/4.0/components/modal/

    Best Regards,

    ChaoDeng

    Thursday, May 27, 2021 9:17 AM
  • User753101303 posted

    Hi,

    Seems you have a data input modal? It seems to me that just closing the modal on success is enough to show the user all went as expected.  For an error, the model would be kept opened and the message shown at the top. IMHO having to dismiss a modal that just tell all went as expected will be quite soon annoying.

    Not directly related, but I discussed that recently (with you?) but IMHO having in a success callback to test the payload (my undertstanding is that you needed help on that) to figure out if all went as expected is quite ugly. If using 400 BadRequest for a model validation error, then scuccess is called ONLY if data were saved and error is called in all other cases. You can then check for a 400 status and use the payload to give additional info to the user or show a geneic message for other cases.

    Thursday, May 27, 2021 9:44 AM
  • User475983607 posted

    Thanks but i want message in Modal popup

    Showing a modal is very simple.  It's one line of code. 

    The programming problem is writing reusable code that fits your application.  This is your responsibility.  Design a message protocol, models, and a JavaScript application to handle the messaging.  This is something you need to think about because it affects your entire application.  

    Chao Deng example solves the original problem.  The alert is a modal.    You created a new issue by refining the requirement.  Rather than asking the community to design and write your code, come up with a design and implement the design.  If you run into problems, post your code.  Explain the expected results and the actual results.

    Thursday, May 27, 2021 10:03 AM