简体   繁体   English

将 LINQ 结果发送到 ASP.NET MVC 中的另一个函数

[英]Send LINQ result to another function in ASP.NET MVC

I am developing a controller in which I consult information and the results are shown in the view of my controller and I send them via email by pressing a "Send Email" button.我正在开发一个控制器,我在其中查询信息,结果显示在我的控制器视图中,我通过按下“发送电子邮件”按钮通过电子邮件发送它们。

Here is an example:下面是一个例子:

例子

But when I want to pass my LINQ results to the SendEmail() method, I get an error.但是,当我想将 LINQ 结果传递给SendEmail()方法时,出现错误。

The LINQ results are: LINQ 结果是:

Issue when i press Send Email button当我按下发送电子邮件按钮时出现问题

Here is my controller这是我的控制器

using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Web.Mvc;

namespace MvcInventory.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Entities db = new Entities();
            var s = from sw in db.TB_RS_PROD.Where(x => x.IDPROD == 1)
                    select sw;
            ViewData["email"] = s;
            return View(s);
        }

        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }

        public ActionResult SendEmail()
        {
            var htmlp = RenderPartialViewToString("~/Views/Home/Index.cshtml", ViewData["email"]);
            SmtpClient SmtpServer = new SmtpClient();
            MailMessage mail = new MailMessage();
            SmtpServer.Credentials = new System.Net.NetworkCredential("IT@serv.com", "");
            SmtpServer.Port = 25;
            SmtpServer.Host = "150.147.12.155";
            mail = new MailMessage();
            mail.From = new MailAddress("IT@serv.com", "IT TEAM");
            mail.To.Add("sz@serv.com");
            mail.Subject = "Request";
            mail.IsBodyHtml = true;
            mail.Body = htmlp;
            mail.Priority = MailPriority.High;
            SmtpServer.Send(mail);
            return View("~/Views/Home/About.cshtml");
        }
    }
}

My view我的看法

@model IEnumerable<MvcInventory.TB_RS_PROD>
@{
}
<html>
<table class="table table-striped">
    <tr>
        <td>ID PRODUCT</td>
        <td>PRODUCT NAME</td>
        <td>QUIANTITY</td>
        <td>DATE</td>
        <td>STATUS</td>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
           <td>@item.IDPROD</td>
           <td>@item.PROD_NAME</td>
           <td>@item.QUANTITY</td>
           <td>@item.UPDATE_DATE</td>
           <td>@item.STATUS</td>
        </tr>
    }
</table>
<button onclick="location.href='@Url.Action("SendEmail","Home")'">Send Email</button>
    </html>

I wanted to do it through ViewData["email"] since I understand that this way the values ​​can be passed.我想通过ViewData["email"]来做,因为我知道这样可以传递值。

Even so, I don't know how to solve this problem.即便如此,我也不知道如何解决这个问题。 It works if i put a static value but i need to put the LINQ result.如果我放置一个静态值但我需要放置 LINQ 结果,它会起作用。

What should I do so that when I click the button I can send the email?我应该怎么做才能在单击按钮时发送电子邮件?

Thank you.谢谢你。

Hi you can use TempData["email"] = s;嗨,您可以使用 TempData["email"] = s; in your Index also use TempData.keep("email") after and in your sendEmail method get this TempData["email"].在您的索引中也使用 TempData.keep("email") 之后和在您的 sendEmail 方法中获取此 TempData["email"]。

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

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