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代わりに、フォームが for submitで機能することを知りたい<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