1

이 양식에서 메일을 보냅니다. 그리고 "보내기"버튼을 누르면 ViewData 키 "Adress"가 "System.String"유형이지만 "IEnumerable"유형이어야합니다. "오류 있음 : "Adress"키가있는 ViewData는 "System.String"유형이지만 "IEnumerable <SelectListItem>"유형이어야합니다.

재미있는 점은 문자가 보냈지 만 응용 프로그램이 떨어졌습니다 ... 제발 도와주세요, 얘들 아!

@Html.DropDownListFor(model => model.Adress, (SelectList)ViewBag.nameEmail, new { style = "width: 310px" }) 
:이 줄

@model MvcLibraly.Models.MailModel 

@{ 
    ViewBag.Title = "Send Mail"; 
} 
<script src="~/Scripts/jquery-1.7.1.min.js"></script> 
<script> 
    $(document).ready(function() { 
     if ('@ViewBag.Message' == 'Sent') { 
      alert('Good!'); 
     } 
    }); 
</script> 
<h2>Send mail</h2> 
<fieldset> 
    <legend>Send Email 
    </legend> 
    @using (@Html.BeginForm("Index", "SendMailer", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" })) 
    { 
    @Html.ValidationSummary() 
     <table> 
      <tr> 

       <td>To: 
       </td> 
       <td> 
        @*@Html.TextBoxFor(m => m.To)*@ 
        @Html.DropDownListFor(model => model.Adress, (SelectList)ViewBag.nameEmail, new { style = "width: 310px" }) 
        @Html.ValidationMessageFor(model => model.Adress, "Error!") 
       </td> 
      </tr> 
      <tr> 
       <td>Subject: 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.Subject) 
        @Html.ValidationMessageFor(model => model.Subject, "Error!") 
       </td> 
      </tr> 
      @*<tr> 
       <td>Attachment 
       </td> 
       <td> 
        <input type="file" name="fileUploader" /> 
       </td> 
      </tr>*@ 
      <tr> 
       <td>Body: 
       </td> 
       <td> 
        @Html.TextAreaFor(model => model.Body) 
        @Html.ValidationMessageFor(model => model.Body, "Error!") 
       </td> 
      </tr> 
     </table>  


     <input type="submit" value="Send" /> 
    } 
</fieldset> 

오류 :

여기
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Net.Mail; 
using System.Web; 
using System.Web.Mvc; 
using System.Data; 
using System.Data.Entity; 
using MvcLibraly.Models; 
using System.Web.Mvc.Html; 
using System.Web.UI.WebControls; 


namespace MvcLibraly.Controllers 
{ 
    public class SendMailerController : Controller 
    { 
     private BookDBContext db = new BookDBContext(); 
     // 
     // GET: /SendMailer/ 

     public ActionResult Index(string nameEmail) 
     { 



      var EmailLst = new List<string>(); 

      var EmailQry = from f in db.Uchets 
          orderby f.Adress where f.DateVoz < DateTime.Now 
          select f.Adress; 
      EmailLst.AddRange(EmailQry.Distinct()); 
      ViewBag.nameEmail = new SelectList(EmailLst); 



      return View(); 
     } 

     /// <summary> 
     /// Send Mail with Gmail 
     /// </summary> 
     /// <param name="objModelMail">MailModel Object, keeps all properties</param> 
     /// <param name="fileUploader">Selected file data, example-filename,content,content type(file type- .txt,.png etc.),length etc.</param> 
     /// <returns></returns> 
     [HttpPost] 
     public ActionResult Index(MvcLibraly.Models.MailModel objModelMail, HttpPostedFileBase fileUploader) 
     { 





      if (ModelState.IsValid) 
      { 


       string from = "[email protected]"; //example:- [email protected] 
       using (MailMessage mail = new MailMessage(from, objModelMail.Adress)) 
       { 
        mail.Subject = objModelMail.Subject; 
        mail.Body = objModelMail.Body; 
        if (fileUploader != null) 
        { 
         string fileName = Path.GetFileName(fileUploader.FileName); 
         mail.Attachments.Add(new Attachment(fileUploader.InputStream, fileName)); 
        } 
        mail.IsBodyHtml = false; 
        SmtpClient smtp = new SmtpClient(); 
        smtp.Host = "smtp.gmail.com"; 
        smtp.EnableSsl = true; 
        NetworkCredential networkCredential = new NetworkCredential(from, "mypassword"); 
        smtp.UseDefaultCredentials = true; 
        smtp.Credentials = networkCredential; 
        smtp.Port = 587; 
        smtp.Send(mail); 
        ViewBag.Message = "Sent"; 
        return View("Index", objModelMail); 
       } 
      } 
      else 
      { 
       return View(); 
      } 
     } 
    } 
} 

보기입니다 :

여기
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MvcLibraly.Models 
{ 
    public class MailModel 
    { 
     public string To { get; set; } 
     public string Adress { get; set; } 
     public string Subject { get; set; } 
     public string Body { get; set; } 

    } 
} 

이 컨트롤러입니다 : 여기

내 모델입니다

업데이트 !!!

컨트롤러 :

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Net.Mail; 
using System.Web; 
using System.Web.Mvc; 
using System.Data; 
using System.Data.Entity; 
using MvcLibraly.Models; 
using System.Web.Mvc.Html; 
using System.Web.UI.WebControls; 


namespace MvcLibraly.Controllers 
{ 
    public class SendMailerController : Controller 
    { 
     private BookDBContext db = new BookDBContext(); 
     // 
     // GET: /SendMailer/ 

     private List<SelectListItem> GetEmailList() 
{ 
    var emailLst = (from f in db.Uchets 
        orderby f.Adress 
        where f.DateVoz < DateTime.Now 
        select new SelectListItem 
        { 
         Text = f.Adress, 
         Value = f.Adress 
        }).Distinct().ToList(); 

    return emailLst; 
} 

public ActionResult Index(string nameEmail) 
{ 
    ViewBag.nameEmail = GetEmailList(); 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(MvcLibraly.Models.MailModel objModelMail, HttpPostedFileBase fileUploader) 
{ 
    if (ModelState.IsValid) 
    { 
     // ... 
    } 
    else 
    { 
     ViewBag.nameEmail = GetEmailList(); 
     return View(); 
    } 
} 

보기 :

@model MvcLibraly.Models.MailModel 

@{ 
    ViewBag.Title = "Send Mail"; 
} 
<script src="~/Scripts/jquery-1.7.1.min.js"></script> 
<script> 
    $(document).ready(function() { 
     if ('@ViewBag.Message' == 'Sent') { 
      alert('Good!'); 
     } 
    }); 
</script> 
<h2>Send mail</h2> 
<fieldset> 
    <legend>Send Email 
    </legend> 
    @using (@Html.BeginForm("Index", "SendMailer", FormMethod.Post, new { @id = "form1", @enctype = "multipart/form-data" })) 
    { 
    @Html.ValidationSummary() 
     <table> 
      <tr> 

       <td>To: 
       </td> 
       <td> 
        @*@Html.TextBoxFor(m => m.To)*@ 
        @Html.DropDownListFor(model => model.Adress, (IList<SelectListItem>) ViewBag.nameEmail, new { style = "width: 310px" }) 
        @Html.ValidationMessageFor(model => model.Adress, "Error!") 
       </td> 
      </tr> 
      <tr> 
       <td>Subject: 
       </td> 
       <td> 
        @Html.TextBoxFor(model => model.Subject) 
        @Html.ValidationMessageFor(model => model.Subject, "Error!") 
       </td> 
      </tr> 
      @*<tr> 
       <td>Attachment 
       </td> 
       <td> 
        <input type="file" name="fileUploader" /> 
       </td> 
      </tr>*@ 
      <tr> 
       <td>Body: 
       </td> 
       <td> 
        @Html.TextAreaFor(model => model.Body) 
        @Html.ValidationMessageFor(model => model.Body, "Error!") 
       </td> 
      </tr> 
     </table>  


     <input type="submit" value="Send" /> 
    } 
</fieldset> 

답변

0

ViewBag.nameEmail가 를 IEnumerable < SelectListItem>해야합니다, 그래서 이것은 당신의 HttpGet 행동에 고정되어야한다. 또한 HttpPost 작업에서 모델이 유효하지 않은 경우 ('else'부분) 전자 메일 목록을 다시 반환해야합니다. 그렇지 않으면 ViewBag.nameEmail이 null 또는 비어 있습니다. 이 컨트롤러의 수정입니다 :

private List<SelectListItem> GetEmailList() 
{ 
    var emailLst = (from f in db.Uchets 
        orderby f.Adress 
        where f.DateVoz < DateTime.Now 
        select new SelectListItem 
        { 
         Text = f.Adress, 
         Value = f.Adress 
        }).Distinct().ToList(); 

    return emailLst; 
} 

public ActionResult Index(string nameEmail) 
{ 
    ViewBag.nameEmail = GetEmailList(); 
    return View(); 
} 

[HttpPost] 
public ActionResult Index(MvcLibraly.Models.MailModel objModelMail, HttpPostedFileBase fileUploader) 
{ 
    if (ModelState.IsValid) 
    { 
     // ... 
    } 
    else 
    { 
     ViewBag.nameEmail = GetEmailList(); 
     return View(); 
    } 
} 

... 이것은보기에 수정 프로그램입니다 :

@Html.DropDownListFor(model => model.Adress, (IList<SelectListItem>) ViewBag.nameEmail, new { style = "width: 310px" }) 

희망 도움)

+0

같은 라인에서 같은 오류가 발생했습니다. .. – user3440844

+0

정확한 코드를 지금 사용하십시오. –

+0

업데이트 된 게시물! – user3440844