简体   繁体   English

在控制器中记录插入失败的成功时如何显示消息或警报

[英]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. 在我的_Layout.cshtml页面中,有一个文本框允许用户键入他/她的电子邮件地址(这是用于新闻通讯)。如果存在键入的电子邮件地址,则它不会插入数据库,如果不是,则插入数据库。 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. 如果成功或错误,它将返回到同一_Layout.csthml页面。 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. 在视图中,您可以添加jquery以显示消息。 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 您将必须将信息传递给控制器​​中的viewmodel

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

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