开发者

How to make my template respect meta tags?

I am trying to override the DateTime template since I need custom class names on it.

So I have this in my edittemplates/datetime.cshtml

@model System.DateTime

@Html.TextBox("", Model, new { @class = "date-picker" })

// in my view model

  [DataType(DataType.Date)]
  public DateTime Date { get; set; }

So I am not sure how to make it just use my meta tag and use the date only. Yes I could hardcode it in and have Mode.ToShortDate() but maybe I will want ToLongdate() one time.

Edit

I think people are getting confused. First ignore the class name I just threw it in their to show why I am overriding the default datetime.

For me hardcoding these class names don't seem like a solution to me and that why I am struggling to figure out if I really want to use these templates and instead just go back to a using textboxfor().

The problem with that is then I have to change "Date" to a string as I only want to show the "Date" part. So now I have开发者_如何学运维 to later on convert the string back to a datetime so I can insert it into my db. With the templates I could leave it as a datetime and save a conversion.

For me it is weird to think that every datetime one will need the same class names and same html properties. These editorFor() should really take in html properties.

Second my whole post was this.

If I don't override the datetime template and do this

  [DataType(DataType.Date)]
  public DateTime Date { get; set; }

then the rendered result will only show the date part. Now if I override the datetime so I can add my custom stuff to it and do what I have above it will not respect the meta tag and will render the full datetime.

So my question is how do I make it so it uses the meta tag? Since not every date field that I have will only show the date. So hard coding something like that into the template would be dumb.


I have just recreated your example and it appears to work as expected.

In your View are you binding the Date property as normal?

@Html.EditorFor(model => model.Date)

If you have this then it should work as you want it to. However if it still isn't working then I can only think of trying one other thing. On your model add the following attribute;

[UIHint("ToLongDate")]
public DateTime Date { get; set; }

Then create a new PartialView in your edittemplates folder called ToLongDate.cshtml and add the following lines;

@model System.DateTime
@Html.TextBox("", Model.ToLongDateString(), new { @class = "date-picker" })

Using this technique you could create various date format varitations and have each property using different ones.


I think the easiest way to do it is to use an overload of EditorFor() that accepts additional view data. You can use that overload to send view data to the template. You could send a date format string and then render the textbox like this:

@Html.TextBox("", Model.ToString(ViewData["format"].ToString()), new { @class = "date-picker" })

Here are all the possible date format string http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx


ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery validate date range validator - Stuart Leeks - Site Home - MSDN Blogs


I know it's an old post but this maybe the answer you need. It's using custom attributes in your model that allow you to add html attributes to your view

http://aspadvice.com/blogs/kiran/archive/2009/11/29/Adding-html-attributes-support-for-Templates-2D00-ASP.Net-MVC-2.0-Beta_2D00_1.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜