1

I have DisplayFor Field where I show data from table.

I need to send Email to User (using his Email that are my value in DisplayFor)

I wrote this code in Controller to Send Email.

Here it is:

      public ActionResult SendEmail(SmartSolutions.Models.Clients _objModelMail, string email)
    {        
            MailMessage mail = new MailMessage();
            mail.To.Add(email);
            mail.From = new MailAddress(_objModelMail.From = "[email protected]");
            mail.Subject = _objModelMail.Subject = "Данные для входа";
            string Body = _objModelMail.Body = "Ваши данные для входа это ваш Email"+" Ваш пароль"+_objModelMail.Password;
            mail.Body = Body;
            mail.IsBodyHtml = true;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("********", "*********");// Enter seders User name and password
            smtp.EnableSsl = true;
            smtp.Send(mail);
            return View("SendEmail", _objModelMail );


    }

It works. I try to send value from DisplayFor to method this is string email

So in view I have this code

  <td style="text-align: center;">
    @Html.DisplayFor(modelItem => item.Email,null,"email")
  </td>

But when I tap submit button it says me this enter image description here

If i change DisplayFor to EditorFor, all ok.

Model of Clients

 public int ID { get; set; }
    [Required]
    [Display(Name = "Email")]
    [EmailAddress]
    public string Email { get; set; }
    [Display(Name = "Ф.И.О")]
    public string UserName { get; set; }
    [Display(Name = "Должность")]
    public string Position { get; set; }
    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Пароль")]
    public string Password { get; set; }
    public string From { get; set; }
    public string Subject { get; set; }
    public string Body { get; set; }

How I can do this for DisplayFor ?

6
  • Please edit the question, don't share your password in the code for the Email ID(in case that is real password...) Commented Mar 11, 2017 at 14:56
  • can you show model of Clients ? Commented Mar 11, 2017 at 15:44
  • Updated my post@Usman Commented Mar 11, 2017 at 15:47
  • and are you getting null in _objModelMail? and email? Commented Mar 11, 2017 at 15:53
  • yes, when I use DisplayFor@Usman Commented Mar 11, 2017 at 15:54

2 Answers 2

1

actually @Html.DisplayFor() does only one way trip means it will only show the value not the other way around and and when you submit form it submits value of hidden field , textbox etc and if you want to define a custom field and get that value in action you can use @Html.Hidden and use it like this

@Html.Hidden("clientEmail",item.Email) the first overload sets the name of hidden field and second overload sets the value of hidden field and finally in action use

 public ActionResult SendEmail(SmartSolutions.Models.Clients _objModelMail, string clientEmail)
    {   

    }
Sign up to request clarification or add additional context in comments.

Comments

0

In the view, add this line:

@Html.HiddenFor(modelItem => item.Email);

This way, the value of the property won't be lost when your post action in the controller (SendEmail) is called.

Next thing you need to do, is to remove the email parameter from your controller. Since the model now carries the value in the property, you can actually use your model, so change this line in the controller:

mail.To.Add(_objModelMail.Email);    // was mail.To.Add(email);

1 Comment

Not works. I do it like this @Html.DisplayFor(modelItem => item.Email,null,"email") @Html.HiddenFor(modelItem => item.Email)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.