ASP.NET 视2编辑模板值的类型,int
-
11-07-2019 - |
题
我想创建一个软2编辑模板值类型,即int有人做这个的预览1位?
非常感谢
解决方案
我没有试图预览1,但他们做了什么,你都要求在这channel9视频:
他们这样做两个DisplayFor和EditorFor,开始围绕2分钟。
-编辑--
值的类型,即int我能得到它的工作在相同的方式。
创建一个模型,通过对我的看法:
public class HomeController : Controller
{
public ActionResult Index()
{
HomeModel model = new HomeModel();
model.message = "Welcome to ASP.NET MVC!";
model.number = 526562262;
model.Date = DateTime.Now;
return View(model);
}
}
public class HomeModel
{
public string message { get; set; }
public int number { get; set; }
public DateTime Date { get; set; }
}
链接查看,以使用该模型的新的模板的逻辑:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HomeModel>" %>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<p>
<% Html.EditorFor(c => c.message); %>
</p>
<p>
<% Html.EditorFor(c => c.number); %>
</p>
<p>
<% Html.EditorFor(c => c.Date); %>
</p>
然后创建一个模板,用于各种类型如Int32:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Editor For My Int32: <%= Html.TextBox("abc", Model.ToString())%>
我把这个放在美景\共享\EditorTemplates\Int32。ascx
其他提示
威尔尼克·克拉克的,当你在回发提交值答案的工作?
在MVC2预览2,调用Html.Textbox( “ABC”,Model.ToString()) 将呈现与“名为.abc”附加到名称,例如一个文本框
<input id="StartDate_abc" name="StartDate.abc" type="text" value="02 Feb 09" />
当您回传和这将导致问题尝试的UpdateModel()。
我做了一个DateTime编辑模板,对我下面的作品:
/Views/Shared/EditorTemplates/DateTime.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %>
或者,使用jQuery的DatePicker的所有的DateTime是否 添加到jQuery和jQueryUI的一个参考或者您的母版或含有该呼叫到EditorFor视图。
/Views/Shared/EditorTemplates/DateTime.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox("", Model.ToString("dd MMM yy")) %>
<script type="text/javascript">
$("#<%= ViewData.ModelMetadata.PropertyName %>").datepicker({ dateFormat: 'dd M y' });
</script>
<强>更新:ASP.NET MVC3 下,使用剃刀语法:
@model System.DateTime
@Html.TextBox("", Model.ToString("dd MMM yy"))
<script type="text/javascript">
$("#@ViewData.ModelMetadata.PropertyName").datepicker({ dateFormat: 'dd M y' });
</script>
和使用它所有你在查看需要的是:
@Html.EditorFor(model => model.DueDate)
-Matt
我已经写一个博客帖子有关如何在MVC 2创建可重用的模板来做到这一点。
我的交也解释TemplateInfo
和模板之间的关系。
不隶属于 StackOverflow