-2

I have setup an asp.net MVC project that inserts a record and then grabs the url information to be sent via email of which is a link in the body of the email. I would like to be able to pass the location of the details page with the id of the record if that makes sense? I am just not sure how to capture the full path and the id that I just created in the insert of the record just submitted. Can any one please help me on this. It would be greatly appreciated. This is my code and I need the url and id to be in the body of the email upon submit:

 [HttpPost]
    public ActionResult NewHire([Bind(Include = "ID,Manager,HR_Emp,Emp_FirstName,Emp_LastName,Emp_StartDate,Emp_OfficeLocation,Emp_Department,Emp_Title")] NewHire newhire)
    {
        if (ModelState.IsValid)
        {
            _entities.NewHires.Add(newhire);
            _entities.SaveChanges();

            MailMessage mail = new MailMessage();
            mail.To.Add("Stephen.Michaels@brixmor.com");
            mail.From = new MailAddress("someone@somewhere.com");
            mail.Subject = "Test";
            string Body = "<a href=http://www.google.com>" + "Click for Record" + "</a>";
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "test";
            smtp.Port = 25;
            smtp.Send(mail);
            return RedirectToAction("NewHire");


        }

        return View(newhire);
    }
sshackles
  • 143
  • 1
  • 4
  • 17

1 Answers1

0

I would suggest you to use the Url.Action

    if (ModelState.IsValid)
    {
        _entities.NewHires.Add(newhire);
        _entities.SaveChanges();
        string url = Url.Action("ActionName", "ControllerName", newhire.id);

        MailMessage mail = new MailMessage();
        mail.To.Add("Stephen.Michaels@brixmor.com");
        mail.From = new MailAddress("someone@somewhere.com");
        mail.Subject = "Test";
        string Body = "<a href='"+ url +"'>" + "Click for Record" + "</a>";
        mail.Body = Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "test";
        smtp.Port = 25;
        smtp.Send(mail);
        return RedirectToAction("NewHire");


    }
Vsevolod Goloviznin
  • 12,074
  • 1
  • 49
  • 50
Mathieu Labrie Parent
  • 2,598
  • 1
  • 9
  • 10
  • This is working great but how about getting the http://localhost:5000 in the url? – sshackles Dec 10 '14 at 21:07
  • @sshackles check this question on how to build an absoulte uri: http://stackoverflow.com/questions/7406258/getting-absolute-url – Vsevolod Goloviznin Dec 10 '14 at 21:24
  • 1
    Actually a neat trick is that if you simply pass the protocol parameter in `Url.Action`, it will build an absolute URI automatically: `Url.Action("Action", "Controller", new { id = newhire.id }, "http")`. You can also use `Request.Url.Scheme` instead of hard-coding the protocol to have it use whichever is in use currently. – Chris Pratt Dec 10 '14 at 21:40