简体   繁体   English

当用户单击视图中的按钮时,如何从控制器为列表中的每个项目调用方法

[英]How to call method from controller for each item in a list when the user click a button in View

I'm using ASP.Net MVC with EF6, I want to enable user to send email for list of students by click on send button in View then the message will sent to each student in the list (students email address are stored in the list ). 我正在使用带有EF6的ASP.Net MVC,我想允许用户通过单击“视图”中的“发送”按钮来发送电子邮件给学生列表,然后消息将发送给列表中的每个学生(学生的电子邮件地址存储在列表中) )。 the user will enter his name, email and message, 用户将输入他的姓名,电子邮件和消息,

How to call Contact method from controller for each student when the user click send in View? 用户单击视图中的发送时,如何从控制器为每个学生调用联系人方法?

    public ActionResult Project(int? id)
    {
         mytable s = new mytable()
        {
        //this student list contains student information (name, email .. ) 
        Student = (from ss in db.Student
                        join sp in db.Stu_Projects on ss.studentId equals 
                        sp.StuId
                        where sp.PId == id
                        select ss).ToList()
        };
     return View(s);

    }

    //I want to call this method for each student in the list when the user click send button in the view 
    [HttpPost] 
    [ValidateAntiForgeryToken]
    public async Task ContactAsync(String FromName, String FromEmail, String Message,String to)
    {
        if (ModelState.IsValid)
        {
            var body = ".....";
            var message = new MailMessage();
            message.To.Add(new MailAddress(to));  // receiver (each student in the list) 
            message.Subject = "Testing";
            message.Body = string.Format(body, FromName, FromEmail, Message);
            message.IsBodyHtml = true;
            using (var smtp = new SmtpClient())
            {
                await smtp.SendMailAsync(message);
            }
        }
    }

 @model -------- @{ /**/ ViewBag.Title = "Project";} <form> Email <input type="text" name="email" placeholder="your email..." id="from"> Subject <input type="text" name="subject" placeholder="Subject..." id="contact-subject"> Message <textarea name="message" placeholder="Message..." id="contact-message"></textarea> <button type="submit" class="btn">Send message</button> </form> 

Your inputs name should have the same name like its named in attributes at your function ContactAsync 输入名称应与函数ContactAsync中的属性中的名称相同

<input name="FromName"/> 

and so on.. 等等..

also you can read msdn enter link description here 你也可以阅读msdn 在这里输入链接描述

you try this below razor view . 您可以在razor视图下尝试此操作。

 @model  --------
    @{
        /**/
    ViewBag.Title = "Project"; 
    }
    <form>
    Email
    <input type="text" name="email" placeholder="your email..." id="email">
    Subject
    <input type="text" name="contact-subject" placeholder="Subject..." id="contact-subject">
    Message
    <textarea name="contact-message" placeholder="Message..." id="contact-message"></textarea>                       
    <button type="submit" class="btn">Send message</button>
    </form>

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

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