Asp.net mvc 如何在mvc中向电子邮件添加参数
大家好,我已经创建了一个实体,我已经使用一个模型创建了一个视图,“客户机”,现在我已经尝试通过视图中输入的字段发送电子邮件 这就是我所拥有的,出于安全原因,我只在测试中使用了一个字段,端口号、主机等都是随机文本 观点:Asp.net mvc 如何在mvc中向电子邮件添加参数,asp.net-mvc,html,asp.net-mvc-3,asp.net-mvc-4,Asp.net Mvc,Html,Asp.net Mvc 3,Asp.net Mvc 4,大家好,我已经创建了一个实体,我已经使用一个模型创建了一个视图,“客户机”,现在我已经尝试通过视图中输入的字段发送电子邮件 这就是我所拥有的,出于安全原因,我只在测试中使用了一个字段,端口号、主机等都是随机文本 观点: @model _2.Models.Client @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <
@model _2.Models.Client
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Client</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Title)
@Html.ValidationMessageFor(model => model.Title)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Initials)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Initials)
@Html.ValidationMessageFor(model => model.Initials)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
即使我在字段中输入了文本,它仍会拾取空值,请帮助我解决此问题谢谢您的时间
我创建了一个客户端实例,但它只发送带有正文的空白电子邮件,没有发送在编辑器字段中输入的值
ActionResult
,但从未出现过错误
异步任务
model
,Client
BeginForm()
也缺少您的操作和控制器,但我确信您刚刚排除了这些李>
调试提示:
- 打开您的web浏览器网络视图。“Chrome中的Ctrl+Shift+j'。然后单击“创建”按钮。当您打开创建发送的数据包时。您看到模型的所有字段了吗?(即
)Client.Title=“fancy Title”
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Contact(Client model)
{
try
{
if (ModelState.IsValid)
{
var body = "<p>Email From: {0} ({1})</p><p>Message:</p><p>{2}</p>";
var message = new MailMessage();
message.To.Add(new MailAddress("test.co.za"));
message.To.Add(new MailAddress("test.co.za"));
message.From = new MailAddress("test.co.za");
message.Subject = "Your email subject";
message.Body = string.Format(body, model.Title, model.Initials, model.FirstName);
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
{
smtp.Host = "host";
smtp.Port = port number;
await smtp.SendMailAsync(message);
smtp.Dispose();
return RedirectToAction("Sent");
};
}
}
return View(model);
}
catch (NullReferenceException ex)
{
return Content("Data added successfully" + ex);
}
}
public ActionResult Sent()
{
return View();
}
System.NullReferenceException: Object reference not set to an instance of an object