联系人表格不起作用,“值不能为空。参数名称:地址” MVC5

时间:2018-09-27 17:13:20

标签: c# asp.net asp.net-mvc

我有联系表,对出什么问题一无所知。

这是我编写的模型的代码:

public class Contact
{
    public string FromName { get; set; } 
    public string FromEmail { get; set; } 
    public string Subject { get; set; } 
    public string Message { get; set; } 
}

我通过读/写操作创建的控制器:

using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using GJL.Models;

namespace GJL.Controllers
{
    public class ContactController : Controller
    {
        // GET: Contact
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(Contact vm)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    MailMessage msz = new MailMessage();
                    msz.From = new MailAddress(vm.FromEmail);//Email which you are getting 
                                                         //from contact us page 
                    msz.To.Add("roykooiman67@gmail.com");//Where mail will be sent 
                    msz.Subject = vm.Subject;
                    msz.Body = vm.Message;
                    SmtpClient smtp = new SmtpClient();

                    smtp.Host = "smtp.gmail.com";

                    smtp.Port = 465;

                    smtp.Credentials = new System.Net.NetworkCredential
                    ("roykooiman67@gmail.com", "PasswordGmail");

                    smtp.EnableSsl = true;

                    smtp.Send(msz);

                    ModelState.Clear();
                    ViewBag.Message = "Bedankt voor je bericht!";
                }
                catch (Exception ex)
                {
                    ModelState.Clear();
                    ViewBag.Message = $" Er ging iets mis: {ex.Message}";
                }
            }

            return View();
        }

        public ActionResult Error()
        {
            return View();
        }
    }
}

我的观点(在pastebin中可能太长了):

https://pastebin.com/LvtSzY4V

这是我得到的错误:

  

值不能为null。参数名称:地址

我读到它必须与凭据有关。但这是我帐户的正确凭据。

1 个答案:

答案 0 :(得分:0)

例如,在输入控件上将asp-for属性替换为name属性后,问题已解决。

    <input name="FromName" class="form-control" />
    <input name="FromEmail" class="form-control" />
    <input name="Subject" class="form-control" />
    <textarea rows="5" cols="15" name="Message" class="form-control"></textarea>

asp-for标记助手在ASP.NET Core MVC中引入,在MVC5中不可用