0

我的网站上有反馈表,看起来像 在此处输入图像描述

我为我的表格创建了模型

 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>
}

我想知道我的表单是否可以a用于提交<input type="submit" />

而且我不知道如何制作附件,我试着制作

   [HttpGet]
    public ActionResult Feedback()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Feedback(FeedbackForm Model)
    {

        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.BodyEncoding = Encoding.UTF8;

        msg.From = new MailAddress(Model.Email, @Resources.Global.Feedback_Email_Title);
        msg.To.Add("tayna-anita@mail.ru");

        string message = @Resources.Global.Feedback_Name + ": " + Model.Name + "\n"
                        + @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
                        + @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
                        + @Resources.Global.Feedback_Company + ": " + Model.Company + "\n\n"
                        + Model.AdditionalInformation;
        msg.Body = message;
        msg.IsBodyHtml = false;

        //Attachment
        if (Model.ProjectInformation != null)
        {
            HttpPostedFileBase attFile = Model.ProjectInformation;
            int attachFileLength = attFile.ContentLength;
            if (attachFileLength > 0)
            {
                string strFileName = Path.GetFileName(Model.ProjectInformation.FileName);
                Model.ProjectInformation.SaveAs(Server.MapPath(strFileName));
                MailAttachment attach = new MailAttachment(Server.MapPath(strFileName));
                msg.Attachments.Add(attach);
                string attach1 = strFileName;
            }
        }

        SmtpClient client = new SmtpClient("smtp.mail.ru", 25);
        client.UseDefaultCredentials = false;
        client.EnableSsl = false;

        try
        {
            client.Send(msg);
        }

        catch (Exception ex)
        {
        }

        FeedbackForm tempForm = new FeedbackForm();
        return View(tempForm);
    }

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

4

1 回答 1

1

好吧,回答您的第一个问题,是的,您可以使用锚标记代替提交输入。

您将希望像这样使用 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));
于 2013-05-21T14:50:56.930 回答