0

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
bill
  • 854
  • 3
  • 17
  • 41
  • Check if this can help you: [How to show Alert Message like “successfully Inserted” after inserting to DB using ASp.net MVC3](http://stackoverflow.com/questions/8063345/how-to-show-alert-message-like-successfully-inserted-after-inserting-to-db-usi) – Shanid Mar 30 '16 at 04:18

4 Answers4

0

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');" };                  
    }
bluetoothfx
  • 643
  • 9
  • 23
0
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>'
samvietnam
  • 16
  • 2
0
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!");                    
 }
}
Fanjo Lama
  • 581
  • 3
  • 15
  • should I do the view part inside the – bill Mar 30 '16 at 04:52
  • When you do return Json() you are specifically telling MVC not to use a view, and to serve serialized JSON data. So if you want to display in a simple View just do return View() and with the help of TempData[] you can show message whatever you want. – Fanjo Lama Mar 30 '16 at 05:04
0

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

Zet
  • 571
  • 3
  • 13
  • 31