如何仅将表单值发送到电子邮件?

时间:2018-08-21 16:25:28

标签: c# asp.net-mvc

我的问题:

运行项目时,我会收到两次电子邮件,一次只有如下标签:

Student ID: 
Student Name:
Student Age:

Student Major:
StudentGPA:

还有另一个具有实际值的信息,我在表格中给出并提交。如下所示:

Student ID: 90
Student Name:zx
Student Age:24

Student Major:2
StudentGPA:3.4

如何停止将标签通过电子邮件发送给我?

如果您愿意,下面是我的代码。谢谢!

控制器

public ActionResult AddStudentInfo(Models.StudentRecord Stu)
{


    List<SelectListItem> paitems = new List<SelectListItem>();
    SelectListItem item1 = new SelectListItem() { Text = "A", Value = "1", Selected = true };
    SelectListItem item2 = new SelectListItem() { Text = "R", Value = "2", Selected = false };
    SelectListItem item3 = new SelectListItem() { Text = "D Plates", Value = "3", Selected = false };

    paitems.Add(item1);
    paitems.Add(item2);
    paitems.Add(item3);
    ViewBag.StudentMajor = paitems;


    string body = "Student ID: " + Stu.studentID + "\n"
                        + "Student Name:" + Stu.studentName + "\n"
                        + "Student Age:" + Stu.studentAge + "\n\n"
                        + "Student Major:" + Stu.studentMajor + "\n"
                        +"StudentGPA:" + Stu.studentGPA + "\n\n";
    MailMessage mailBody = new MailMessage("mmtest@gmail.com","test1@gmail.com");
    mailBody.Subject = "Contact Form";
    mailBody.Body = body;

    SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
    smtpClient.Credentials = new NetworkCredential("test1", "test123!");

    smtpClient.EnableSsl = true;
    smtpClient.Send(mailBody);


    if (ModelState.IsValid)
    {

        connection();
        SqlCommand com = new SqlCommand("InsertData", conn);
        com.CommandType = System.Data.CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@studentID", Stu.studentID);
        com.Parameters.AddWithValue("@studentName", Stu.studentName);
        com.Parameters.AddWithValue("@studentAge", Stu.studentAge);
        com.Parameters.AddWithValue("@studentMajor", Stu.studentMajor);

        com.Parameters.AddWithValue("@studentGPA", Stu.studentGPA);
        conn.Open();

        int i = com.ExecuteNonQuery();
        conn.Close();
        if (i>=1)
        {
            ViewBag.Message = "New Student has been added";

        }

    }
    ModelState.Clear();
    return View();
}

查看

我想与视图没有多大关系。我在那里拥有所有这些属性。但是,这只是“提交”按钮:

<script>
    function myAlert() {
        window.confirm("Student added successfully");
    }
</script>

<div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Add" onclick="myAlert()" class="btn btn-default" />
</div>

模型

[Display(Name = "Student ID")]
[Required(ErrorMessage = "Enter Student ID")]
public string studentID { get; set; }

[Display(Name = "Student Name")]
public string studentName { get; set; }

[Display(Name = "Student Age")]
public string studentAge { get; set; }

[Display(Name = "Student Major")]
public string studentMajor { get; set; }

[Display(Name = "Student GPA")]
public string studentGPA { get; set; }

0 个答案:

没有答案