简体   繁体   中英

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 ). 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

<input name="FromName"/> 

and so on..

also you can read msdn enter link description here

you try this below razor view .

 @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>

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