I have built a small MailApplication for my developing class, it is a homework assignment, but I can't seem to solve this error.
I generated a new Controller (MailController) based on a model (Mail). Everything is working just fine, the mail is being sent, but when I return View("Index", mailModel);, I receive an error when submitting the url/Mail/Create (POST) form.
The model item passed into the dictionary is of type 'WebApplication1.Models.Mail', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[WebApplication1.Models.Mail]'.
The following is the Create method of MailController:
[HttpPost]
[ValidateAntiForgeryToken]
public ViewResult Create([Bind(Include = "ID,From,To,Subject,Body")] Mail mail, Models.Mail mailModel)
{
if (ModelState.IsValid)
{
//Create mail
MailMessage message = new MailMessage();
message.To.Add(mailModel.To);
message.From = new MailAddress(mailModel.From);
message.Subject = mailModel.Subject;
message.Body = mailModel.Body;
message.IsBodyHtml = true;
//Setup host
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password!");
smtp.EnableSsl = true;
//Send the EMail
smtp.Send(message);
return View("Index", mailModel);
}
else
{
return View(mailModel);
}
}
The following is my model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1.Models
{
public class Mail
{
public int ID { get; set; }
public string From { get; set; }
public string To { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
}
And the last thing, my view:
@model IEnumerable<WebApplication1.Models.Mail>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.From)
</th>
<th>
@Html.DisplayNameFor(model => model.To)
</th>
<th>
@Html.DisplayNameFor(model => model.Subject)
</th>
<th>
@Html.DisplayNameFor(model => model.Body)
</th>
<th></th>
</tr>
@foreach (var item in ViewData.Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.From)
</td>
<td>
@Html.DisplayFor(modelItem => item.To)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.Body)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
Mailobject and not a collection. Either change@model IEnumerable<WebApplication1.Models.Mail>to@model WebApplication1.Models.Mailor pass in a list of theMailmodel.@model WebApplication1.Models.Mail, but now there error is CS1597, there is no GetEnumerator() in Mail?@foreach (var item in ViewData.Model) {since you now don't have a list ofMailmodels available. You would need to pass in a list ofMailmodels from your controller such asreturn View(new List<Mail>());