Asp.net mvc asp.net mvc 4来自站点的信函添加附件

Asp.net mvc asp.net mvc 4来自站点的信函添加附件,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我的网站上有反馈表,看起来 我为我的表单创建了一个模型 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace CorePartners_Site2.Models { public class FeedbackForm { pub

我的网站上有反馈表,看起来

我为我的表单创建了一个模型

 using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.Linq;
 using System.Web;

 namespace CorePartners_Site2.Models
 {
     public class FeedbackForm
     {
    public string Name { get; set; } 
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Company { get; set; }
    public string AdditionalInformation { get; set; }

    [FileExtensions(Extensions = "doc,txt,pdf")]
    public HttpPostedFileBase ProjectInformation { get; set; }
     }
 }
并创建了视图

@using (Html.BeginForm("Feedback", "Home", FormMethod.Post, new { id = "feedback-form" }))
{
    @Html.TextBoxFor(model => model.Name, null, new { @class = "text-field" })                   
    @Html.TextBoxFor(model => model.Email, null, new { @class = "text-field" })                   
    @Html.TextBoxFor(model => model.Phone, null, new { @class = "text-field" })
    @Html.TextBoxFor(model => model.Company, null, new { @class = "text-field" })
    @Html.TextAreaFor(model => model.AdditionalInformation, new { cols="1", rows="1" })              
    @Html.TextBoxFor(model => model.ProjectInformation, null, new { type="file", @class="input-file" })
    <a href="#" class="link1" onclick="document.getElementById('feedback-form').submit()"><em><b>Send</b></em></a>
}

但是它在
msg.Attachments.Add(attach)中显示了一个错误似乎不起作用。

要回答第一个问题,可以使用锚定标记代替提交输入

您需要禁用javascript/jquery的标记默认行为,如下所示,然后让它提交表单:

$(function () {
    $('a.something').on("click", function (e) {
        e.preventDefault();
        $('feedback-form').submit();
    });
});
您收到的错误是因为您对msg.attachments.add()方法使用了错误的对象类型。您需要使用附件对象,而不是MailAttachment对象

大概是这样的:

Stream attachmentStream = File.OpenRead (file);
streams.Add (attachmentStream);
mail.Attachments.Add (new Attachment (attachmentStream, Path.GetFileName (file));

您收到的错误是什么?
错误4参数1:无法从“System.Web.Mail.MailAttachment”转换为“System.Net.Mail.Attachment”C:\svn\Corepartners\u SITE\Sources\Version 2\Corepartners\u Site2\Controllers\HomeController.cs 99 41 Corepartners\u Site2
错误3最匹配的重载方法“System.Collections.ObjectModel.Collection.Add(System.Net.Mail.Attachment)”具有一些无效参数C:\svn\Corepartners\U SITE\Sources\Version 2\Corepartners\U Site2\Controller\HomeController.cs 99 21 Corepartners\U Site2
我使用的
System.Web.Mail
System.Net.Mail同时,看起来这是问题的原因,但我不确定。
Stream attachmentStream = File.OpenRead (file);
streams.Add (attachmentStream);
mail.Attachments.Add (new Attachment (attachmentStream, Path.GetFileName (file));