【发布时间】:2017-09-23 16:27:10
【问题描述】:
我正在关注this 教程,使用 Umbraco 和 Mvc 创建一个联系我们的表单 - 我有点不明白为什么某些东西有效。具体来说——ContactForm(局部视图)如何既显示联系我们页面的内容(通过调用@Html.Action("ShowForm", "ContactSurface")),又处理提交按钮(它是如何处理的)知道仅在按下提交按钮时调用 HandleFormPost 吗?)
这是保存“联系我们”网页的视图/模板。
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.ContactHolder>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
Layout = "Master.cshtml";
}
@Html.Action("ShowForm", "ContactSurface")
这是控制器:
public class ContactSurfaceController : Umbraco.Web.Mvc.SurfaceController
{
public ActionResult ShowForm()
{
ContactModel myModel = new ContactModel();
return PartialView("ContactForm", myModel);
}
public ActionResult HandleFormPost(ContactModel model)
{
var newComment = Services.ContentService.CreateContent(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + model.Name, CurrentPage.Id, "ContactFormula");
newComment.SetValue("emailFrom", model.Email);
newComment.SetValue("contactName", model.Name);
newComment.SetValue("contactMessage", model.Message);
Services.ContentService.SaveAndPublishWithStatus(newComment);
return RedirectToCurrentUmbracoPage();
}
}
这是实际显示表单的部分视图,显然还创建了新的 ContactFormula(用于保存联系我们提交的文档类型 - 它包含 emailFrom、contactName 和 contactMessage)。
@inherits Umbraco.Web.Mvc.UmbracoViewPage<ProjectDemo.Models.ContactModel>
@using CohensCigarsDemo.Controllers;
@using (Html.BeginUmbracoForm<ContactSurfaceController>("HandleFormPost"))
{
<label for="emailFrom">E-mail</label> @Html.TextBoxFor(x => x.Email, new { @class = "emailFrom", placeholder = "E-mail" })
<br />
<label for="nameFrom">Name</label> @Html.TextBoxFor(x => x.Name, new { @class = "nameFrom", placeholder = "Name" })
<br />
<label for="messageFrom">Message</label>
<br />
@Html.TextAreaFor(x => x.Message, new { @class = "messageFrom", placeholder = "Message" })
<br />
<input type="submit" value="Submit" />
}
【问题讨论】:
标签: c# asp.net-mvc razor umbraco