0
我有一個頁面,它有一個窗體,所有東西都能正常工作。 我的問題是,當我的模型狀態是無效的,那麼如何恢復與我們聯繫,索引視圖不創建視圖如何使用ASP.net將對象傳遞到特定視圖MVC
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Email,Phone,Message,Date")] Contact_US contact_US)
{
if (ModelState.IsValid)
{
db.Contact_US.Add(contact_US);
db.SaveChanges();
MailMessage mail = new MailMessage();
mail.To.Add("");
mail.From = new MailAddress("");
mail.Subject = contact_US.Email;
string body = contact_US.Message;
mail.Body = "Phone:" + contact_US.Phone + "<br />Name:" + contact_US.Name +"<br />Email:"+contact_US.Email+ "<br /><br />" + body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential
("", "");// Enter seders User name and password
smtp.EnableSsl = true;
smtp.Send(mail);
TempData["ResultMessage"] = "Thank you for your enquiry and or enrolment. we will attend to this submission and come back to you soon";
TempData["TimeMessage"] = "Your entry was received on"+DateTime.Now;
return RedirectToAction("Index");
}
return View(contact_US);
}
在這種情況下,如果模型狀態無效,它看起來脫穎而出創造的看法,但我不我想創建一個視圖。我只想帶我們聯繫索引頁面。
這是我的索引視圖
@using (Html.BeginForm("Create", "Contact_Us", FormMethod.Post, new { @id = "contactusform" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Phone, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12">
@Html.EditorFor(model => model.Phone, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Phone, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group hidden">
@Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2 txtformat" })
<div class="col-md-12 contactusmsg">
@Html.TextAreaFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group contactuspostbtn">
<div class="col-md-12">
<input id="postcontactusmessage" type="submit" value="Send" class="btn btn-default" />
</div>
</div>
</div>
}
感謝所有幫助