[英]ASP.NET MVC 2 editor template for value types, int
I want to create a MVC 2 editor template for a value type ie int , has anyone done this with the preview 1 bits? 我想为值类型创建一个MVC 2编辑器模板,即int,是否有人使用预览1位完成此操作?
Many thanks 非常感谢
Will Nick Clarke's answer work when you submit the values on postback? 当您在回发时提交值时,Nick Clarke的回答是否有效?
In MVC2 preview 2, calling Html.Textbox("abc", Model.ToString()) will render a textbox with ".abc" appended to the name, eg 在MVC2预览2中,调用Html.Textbox(“abc”,Model.ToString())将呈现一个文本框,其名称附加“.abc”,例如
<input id="StartDate_abc" name="StartDate.abc" type="text" value="02 Feb 09" />
which will cause problems when you postback and attempt to UpdateModel(). 当你回发并尝试UpdateModel()时会导致问题。
I did an editor template for a DateTime, the following works for me: 我为DateTime做了一个编辑器模板,以下内容适用于我:
/Views/Shared/EditorTemplates/DateTime.ascx: /Views/Shared/EditorTemplates/DateTime.ascx:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<DateTime>" %>
<%= Html.TextBox(String.Empty, Model.ToString("dd MMM yy")) %>
or, to use jQuery's DatePicker for all your DateTimes add a reference to jQuery and jQueryUI to either your Masterpage or to the View containing the call to EditorFor. 或者,为所有DateTimes使用jQuery的DatePicker,将jQuery和jQueryUI的引用添加到您的母版页或包含对EditorFor的调用的视图中。
/Views/Shared/EditorTemplates/DateTime.ascx: /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>
Update: ASP.NET MVC3 , using the Razor syntax: 更新:ASP.NET MVC3 ,使用Razor语法:
@model System.DateTime
@Html.TextBox("", Model.ToString("dd MMM yy"))
<script type="text/javascript">
$("#@ViewData.ModelMetadata.PropertyName").datepicker({ dateFormat: 'dd M y' });
</script>
And to use it all you need in your View is: 要在View中使用它,您需要的只是:
@Html.EditorFor(model => model.DueDate)
-Matt -Matt
I have not tried preview 1 yet but they did what you are asking for in this channel9 video: 我还没有尝试预览1,但他们在这个channel9视频中做了你要求的:
http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-MVC-2-Preview-1-with-Phil-Haack-and-Virtual-Scott/ http://channel9.msdn.com/posts/Glucose/Hanselminutes-on-9-ASPNET-MVC-2-Preview-1-with-Phil-Haack-and-Virtual-Scott/
They do both DisplayFor and EditorFor, starts around 2 minutes. 他们同时执行DisplayFor和EditorFor,大约需要2分钟。
--Edit-- - 编辑 -
For value type ie int I was able to get it to work in the same way. 对于值类型,即int,我能够以相同的方式使其工作。
Create a model to pass to my view: 创建一个模型以传递给我的视图:
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; }
}
Link view to the model using the new template logic: 使用新模板逻辑将视图链接到模型:
<%@ 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>
Then create a template for each of the types eg Int32: 然后为每个类型创建一个模板,例如Int32:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
Editor For My Int32: <%= Html.TextBox("abc", Model.ToString())%>
I put this in Views\\Shared\\EditorTemplates\\Int32.ascx 我把它放在Views \\ Shared \\ EditorTemplates \\ Int32.ascx中
I've written a blog post about how to do this by creating reusable templates in MVC 2. 我写了一篇关于如何通过在MVC 2中创建可重用模板来做到这一点的博客文章 。
My post also explains the relationship between TemplateInfo
and templates. 我的帖子还解释了
TemplateInfo
和模板之间的关系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.