简体   繁体   English

ASP.NET MVC; IEnumerable的 <Model>

[英]ASP.NET MVC; IEnumerable<Model>

I have built a small MailApplication for my developing class, it is a homework assignment, but I can't seem to solve this error. 我为开发中的类构建了一个小型MailApplication,这是一项家庭作业,但似乎无法解决此错误。 I generated a new Controller (MailController) based on a model (Mail). 我基于模型(邮件)生成了一个新的控制器(MailController)。 Everything is working just fine, the mail is being sent, but when I return View("Index", mailModel); 一切正常,正在发送邮件,但是当我return View("Index", mailModel); , I receive an error when submitting the url/Mail/Create (POST) form. ,在提交url / Mail / Create(POST)表单时收到错误消息。

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: 以下是MailController的Create方法:

        [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("something@gmail.com", "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>

As already pointed out by f0x you model and view are not aligned. 正如f0x所指出的,您的模型和视图未对齐。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Ienumerable模型的Asp.net MVC视图在提交时返回null - Asp.net mvc view with Ienumerable model returns null on submit asp.Net MVC中组合视图和IEnumerable模型的问题 - Problems with combined view and IEnumerable model in asp.Net MVC 在ASP.NET MVC 5中可查询到IEnumerable - IQueryable to IEnumerable in ASP.NET MVC 5 在ASP.NET MVC POST中进行模型绑定IEnumerable? - Modelbinding IEnumerable in ASP.NET MVC POST? IEnumerable的ASP.NET MVC EditorTemplate - ASP.NET MVC EditorTemplate for IEnumerable Asp.net Core MVC-在控制器动作中的窗体上发布到控制器IEnumerable模型为空 - Asp.net Core MVC - on form posting to controller IEnumerable model in controller action is empty 如何用我的IEnumerable填充/填充DropDown <Model> 值ASP.NET MVC - How can I fill / populate DropDown with my IEnumerable<Model> value ASP.NET MVC ASP.NET Core MVC中IEnumerable视图模型的格式值如何? - How format values in IEnumerable view model in ASP.NET Core MVC? ASP.NET 核心 MVC 后 model 与 IEnumerable 或列表字段是 null - ASP.NET Core MVC post model with IEnumerable or List fields are null 在 ASP.NET Core MVC Razor 视图中,与@model IEnumerable 相关的问题<namespace.model>对比@model<namespace.model></namespace.model></namespace.model> - In an ASP.NET Core MVC Razor view, issue related to @model IEnumerable<Namespace.Model> Vs @model<Namespace.Model>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM