MVC EditorTemplate或HtmlHelper

时间:2014-12-04 22:07:09

标签: asp.net-mvc html-helper mvc-editor-templates

我正在创建一个显示项目列表的页面,其中一个属性是图片。

现在我正在构建创建视图。
我将添加拖放功能,以便用户轻松添加图片。
但我想编写易于重用的代码。

看起来我可以将代码放在EditorTemplate或HtmlHelper中。但不知道哪一个应该是正确的方式。

对于我的观点,EditorTemplate看起来更容易实现因为只是创建一个视图并且很容易包含Jquery脚本。
HtmlHelper需要创建一个返回IHtmlHelper的函数,所以我必须使用我不熟悉的几个函数创建html字符串(但我可以学习)。

1 个答案:

答案 0 :(得分:0)

编辑模板和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.');}"
            });
        }
    }
}

在视图中添加Helper名称空间的引用并使用它重用它

编辑模板

用于可空的日期时间模型来解析其时间字段

 @model DateTime?
    @if (Model.HasValue)
    {
        @Html.TextBox("", String.Format("{0:t}", Model), new { @class = "timefield" })
    }
    else
    {
        @Html.TextBox("", "", new { @class = "time" })
    }
相关问题