简体   繁体   中英

How to show message or alert when record insertion success of fail in the controller

In my _Layout.cshtml page there is a textbox that allows user to type his/her email address(this is for newsletter).if typed email address exists it does not insert to the database and if not it insert to the database. at the same time if not inserted I want to show an error message and if insert I want to show success message.this is how I insert to the database,

public ActionResult getNewsLetterMail(string N_id, string N_EmailAdd)
    {
        Session["Ealert"] = null;
        Random random = new Random();
        int idONe = random.Next(99, 999);
        int idTwo = random.Next(999, 9999);
        string middle = "menuka";
        string fullID = idONe.ToString() + middle + idTwo.ToString();
        var N_ID = fullID;
        var N_Email = N_EmailAdd;
        TourCenterDBEntities NewsLetterEntities = new TourCenterDBEntities();
        var existing = NewsLetterEntities.News_Letter.Where(l => l.N_Email == N_EmailAdd);
        Debug.WriteLine(existing.Count());
        if (existing.Count() == 0)
        {
            News_Letter NewsLetterDetails = new News_Letter();
            NewsLetterDetails.N_id = N_ID;
            NewsLetterDetails.N_Email = N_Email;
            NewsLetterEntities.News_Letter.Add(NewsLetterDetails);
            NewsLetterEntities.SaveChanges();
            //want to send success text
        }
        else
        {
            //want to send error text                  
        }
        return Json(new { });
    }

if success or error it returns to the same _Layout.csthml page. how can I do that.hope your help.

You can use. return content.

if (existing.Count() == 0)
    {
        News_Letter NewsLetterDetails = new News_Letter();
        NewsLetterDetails.N_id = N_ID;
        NewsLetterDetails.N_Email = N_Email;
        NewsLetterEntities.News_Letter.Add(NewsLetterDetails);
        NewsLetterEntities.SaveChanges();

        //return Content("Your information saved successfully");
        return new JavascriptResult { Script = "alert('Your information saved successfully');" };
    }
    else
    {
        //return Content("Already exist. Please choose another.");
        return new JavascriptResult { Script = "alert('Your information saved successfully');" };                  
    }
public ActionResult getNewsLetterMail(string N_id, string N_EmailAdd)
{
    Session["Ealert"] = null;
    Random random = new Random();
    int idONe = random.Next(99, 999);
    int idTwo = random.Next(999, 9999);
    string middle = "menuka";
    string fullID = idONe.ToString() + middle + idTwo.ToString();
    var N_ID = fullID;
    var N_Email = N_EmailAdd;
    TourCenterDBEntities NewsLetterEntities = new TourCenterDBEntities();
    var existing = NewsLetterEntities.News_Letter.Where(l => l.N_Email == N_EmailAdd);
    Debug.WriteLine(existing.Count());
    string myMessage="";
    if (existing.Count() == 0)
    {
        News_Letter NewsLetterDetails = new News_Letter();
        NewsLetterDetails.N_id = N_ID;
        NewsLetterDetails.N_Email = N_Email;
        NewsLetterEntities.News_Letter.Add(NewsLetterDetails);
        NewsLetterEntities.SaveChanges();
        myMessage="success!";
    }
    else
    {
        myMessage="Failed!";                  
    }
    return Json(myMessage, JsonRequestBehavior.AllowGet);
}

In Views, you can add jquery to display the message. Following is an example to retrieve the message in Views. You can edit the names in your form accordingly.

`<script type="text/javascript">
  $(document).ready(function () {
   $("#yourForm").submit(function (e) {
      e.preventDefault();
        var valid = $("#yourForm").valid();
        if (valid) {
           $.ajax({
            url: "/getNewsLetterMail",
            type: "POST",
            data: {
                   Name: $("#N_id").val(),
                   Email: $("#N_EmailAdd").val(),
                    },
                    success: function (data) {
                        alert(data);
                        reset();
                    }
                });
            }
        });
 });
</script>'
public ActionResult getNewsLetterMail(string N_id, string N_EmailAdd)
{
    Session["Ealert"] = null;
    Random random = new Random();
    int idONe = random.Next(99, 999);
    int idTwo = random.Next(999, 9999);
    string middle = "menuka";
    string fullID = idONe.ToString() + middle + idTwo.ToString();
    var N_ID = fullID;
    var N_Email = N_EmailAdd;
    TourCenterDBEntities NewsLetterEntities = new TourCenterDBEntities();
    var existing = NewsLetterEntities.News_Letter.Where(l => l.N_Email == N_EmailAdd);
    Debug.WriteLine(existing.Count());
    string myMessage="";
    if (existing.Count() == 0)
    {
        News_Letter NewsLetterDetails = new News_Letter();
        NewsLetterDetails.N_id = N_ID;
        NewsLetterDetails.N_Email = N_Email;
        NewsLetterEntities.News_Letter.Add(NewsLetterDetails);
        NewsLetterEntities.SaveChanges();
        myMessage="success";
    }
    else
    {
        myMessage="failed";                  
    }
    return Json(myMessage, JsonRequestBehavior.AllowGet);
}

In your view.

$.post('@Url.Action("getNewsLetterMail", "yourControllerName")', { N_id: N_id, N_EmailAdd: N_EmailAdd }).done(function (data) {
 if (data == "success") {
                alert("Success!");                    
 }
 if( data== "failed")  {
                alert("Failed!");                    
 }
}

I have no experience but I would try something like that: in my viewmodel I would put string info then in my razor view

 @Html.DisplayFor(m=>m.info)

and in controller

if(existing){info="success"}

You would have to pass info to the viewmodel in your controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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