2

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>
4
  • 2
    I sincerely hope that is not your actual gmail credential. Anyway, your error refers to you passing in a single Mail object and not a collection. Either change @model IEnumerable<WebApplication1.Models.Mail> to @model WebApplication1.Models.Mail or pass in a list of the Mail model. Commented Sep 6, 2014 at 20:20
  • The top line of your view shows what it is expecting, either pass that or change the type to what you are passing. Commented Sep 6, 2014 at 20:25
  • @f0x Thank you for pointing that out, forgot to change that. Anyhow, I changed it to @model WebApplication1.Models.Mail, but now there error is CS1597, there is no GetEnumerator() in Mail? Commented Sep 6, 2014 at 20:26
  • The error refers to the part where you are trying to loop through the models @foreach (var item in ViewData.Model) { since you now don't have a list of Mail models available. You would need to pass in a list of Mail models from your controller such as return View(new List<Mail>()); Commented Sep 6, 2014 at 20:29

1 Answer 1

1

As already pointed out by f0x you model and view are not aligned.

You could either change your view's model definition to:

@model WebApplication1.Models.Mail

Or supply the view with a list from the action.

In here you would need to supply the list:

return View(listOfMailModel);
Sign up to request clarification or add additional context in comments.

5 Comments

I changed to @model WebApplication1.Models.Mail, and I use return View("Index", mailModel); so it should work now, right?
The foreach will fail now. What is your intent with it? It seems you are mixing a list and a single item together.
So, I could also remove the foreach part and it would succeed? I'm just trying out to send an email, so if that part works, I'm fine with it.
You could also create a new model with that list and a single 'selected' item.
@JBehrensEr: Did you get it fixed? Need more help?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.