我是MVC的新手,正在尝试建立联系页面。 我为接触部分创建了局部视图,并使用此行将其添加到索引页面上
@Html.Partial("_Contact")
提交表单后,我的代码将执行,而表单无效时,我必须返回一些内容。 我的问题是我必须返回什么才能使其正常工作。 我想将模型返回到我的联系人局部视图,但是我仍然必须显示我的索引页面,因为那里显示了联系人部分。 还是我看错了方向?
这是邮政编码:
[ValidateAntiForgeryToken]
public IActionResult _Contact(EmailFormModel model)
{
if (ModelState.IsValid)
{
var path = _hostingEnvironment.WebRootPath;
string html = System.IO.File.ReadAllText(path);
var message = new MailMessage();
var templatePath = path + _configuration.GetSection("Email.PathToHtmlTemplate").Value;
var smtpHost = _configuration.GetSection("Email.Smtp.Host").Value;
var smtpPort = Convert.ToInt32(_configuration.GetSection("Email.Smtp.Port").Value);
var smtpUser = _configuration.GetSection("Email.Smtp.UserName").Value;
var smtpPass = _configuration.GetSection("Email.Smtp.Password").Value;
var from = _configuration.GetSection("Email.From").Value;
var to = _configuration.GetSection("Email.To").Value;
var cc = _configuration.GetSection("Email.Cc").Value;
var bcc = _configuration.GetSection("Email.Bcc").Value;
var subject = _configuration.GetSection("Email.Subject").Value;
var dic = new Dictionary<string, string>()
{
{ "{{NAME}}", model.Name },
{ "{{TEL}}", model.Telephone },
{ "{{EMAIL}}", model.Email },
{ "{{MESSAGE}}", model.Message },
};
dic.ForEach(x => html = html.Replace(x.Key, x.Value));
foreach (var t in to.Split(';'))
{
message.To.Add(new MailAddress(t));
}
foreach (var t in cc.Split(';'))
{
message.CC.Add(new MailAddress(t));
}
foreach (var t in bcc.Split(';'))
{
message.Bcc.Add(new MailAddress(t));
}
message.From = new MailAddress(from);
message.Subject = subject;
message.Body = html;
message.IsBodyHtml = true;
using (var smtp = new SmtpClient())
{
var credential = new NetworkCredential
{
UserName = smtpUser,
Password = smtpPass
};
smtp.Credentials = credential;
smtp.Host = smtpHost;
smtp.Port = smtpPort;
smtp.Send(message);
return View("EmailSent");
}
}
return View(model);
}
索引视图:
@{
ViewData["Title"] = "Home Page";
}
...
@Html.Partial("_Contact")
联系人视图:
@model EmailFormModel;
@{
ViewData["Title"] = "Contact";
}
<form asp-controller="Home" asp-action="_Contact" method="post" class="form-horizontal" role="form">
<!-- Contact Page Section Start -->
<div id="contact" class="contact-page-sec pt-100 pb-100">
<div class="container">
<row>
<div class="contact-page-sec">
<div class="sec-title">
<h1>Contacteer ons</h1>
<div class="border-shape"></div>
</div>
</div>
</row>
<div class="row">
<div class="col-md-4">
<div class="contact-info">
<div class="contact-info-item">
<div class="contact-info-icon">
<img src="img/icon/phone.png" alt="" />
</div>
<div class="contact-info-text">
<h2>Telefoon</h2>
<span>
<a href='tel:+3245798689'>+(32) 496 56 53 13</a>
</span>
</div>
</div>
</div>
<div class="contact-info">
<div class="contact-info-item">
<div class="contact-info-icon">
<img src="img/icon/envelope.png" alt="" />
</div>
<div class="contact-info-text">
<h2>e-mail</h2>
<span>
<a href='mailto:info@vloerwerken-krekelbergh.be'>info@vloerwerken-krekelbergh.be</a>
</span>
</div>
</div>
</div>
<div class="contact-info">
<div class="contact-info-item">
<div class="contact-info-icon">
<img src="img/icon/map-marker.png" alt="" />
</div>
<div class="contact-info-text">
<h2>Adres</h2>
<span>Engelstraat 60</span>
<span>8211 Aartrijke, België</span>
</div>
</div>
</div>
<div class="social-profile">
<ul>
<li>
<a href="https://www.facebook.com/vloerwerken-krekelbergh-228041574489932/">
<i class="fa fa-facebook"></i>
</a>
</li>
</ul>
</div>
</div>
<div class="col-md-8">
<div class="contact-field">
<h2>Schrijf uw bericht</h2>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="single-input-field">
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="single-input-field">
<input asp-for="Email" class="form-control" type="email"/>
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="single-input-field">
<input asp-for="Telephone" class="form-control" />
<span asp-validation-for="Telephone" class="text-danger"></span>
</div>
</div>
<div class="col-md-12 message-input">
<div class="single-input-field">
<textarea asp-for="Message" class="form-control"></textarea>
<span asp-validation-for="Message" class="text-danger"></span>
</div>
</div>
<div class="single-input-fieldsbtn" style="clear: both;">
<input id="submitBtn" value="Verstuur" type="submit">
</div>
<div id="info"></div>
</div>
</div>
</div>
</div>
</div>
<!-- Contact Page Section End -->
</form>