編輯模板和的HtmlHelper都擁有自己的使用
的HtmlHelper
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace Helpers
{
public static class HTMLHelper
{
public static MvcHtmlString DeleteActionLink(this AjaxHelper helper, string linkText, string ActionName,
object routeValues)
{
return helper.ActionLink(linkText, ActionName, routeValues, new AjaxOptions
{
Confirm = "Are you sure you want to delete this item?",
HttpMethod = "DELETE",
OnSuccess = "function() {window.location.reload();}",
OnFailure = "function() {alert('Your alert.');}"
});
}
}
}
在視圖中添加輔助命名空間的引用,並用它重用
編輯模板
爲空的日期時間模型來解析其時間字段
@model DateTime?
@if (Model.HasValue)
{
@Html.TextBox("", String.Format("{0:t}", Model), new { @class = "timefield" })
}
else
{
@Html.TextBox("", "", new { @class = "time" })
}
如果對於這個項目來說,一個'EditorTemplate'會更容易。如果你希望跨越多個項目,那麼一個HtmlHelper可以被編譯到一個你可以參考的單獨的dll中。在任何情況下,不要包含腳本'EditorTemplate'(它們屬於主視圖 - 並且您將包含腳本的多個副本 - 每個項目一個!) – 2014-12-06 02:31:31
謝謝@StephenMuecke聲音很好。但在創建DLL的情況下,你如何說這個DLL需要這個script.js的工作?我的意思是我可以在主頁面添加腳本,否則我知道不會工作。只是想知道我如何文件或標記DLL? – 2014-12-08 16:12:59