2012-12-05 4 views
9

Ich habe ASP.NET MVC3 installiert. Ich brauche Datumsauswahl mit formatiertem Datum. Ich habe versucht, diese, aber es funktioniert nicht (beim Passieren "{0: dd/MM/JJJJ}" als Formatparameter, es immer noch nicht-Format):Wie formatieren Sie eine DateTime, die von TextBoxFor() in MVC3 angezeigt wird?

private static MvcHtmlString FormattedDateTextBoxFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string format, RouteValueDictionary htmlAttributes) 
    { 
     var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData); 

     if (metadata.Model != null && metadata.Model as DateTime? != null) 
      htmlAttributes.Add("value", string.Format(format, (DateTime)metadata.Model)); 

     return htmlHelper.TextBoxFor(expression, htmlAttributes); 
    } 

EDIT: Mein Code funktioniert, wenn Format ist „{ 0: dd-MM-yyyy} "aber nicht nur für" {0: TT/MM/JJJJ} "

Ich weiß, dass MVC4 diese Funktionalität bereits hat, aber leider ist mein Projekt auf MVC3 geschrieben. Kannst du mir helfen?

+1

Mögliches Duplikat von [Nur Datum von TextBoxFor()] (http://StackOverflow.com/questions/1961114/date-only-from-textboxfor) – KyleMit

Antwort

1

in Ihrem Modell verwenden Sie die folgenden Schritte aus:

[DisplayFormat(DataFormatString="{0:dd/MM/yyyy}")] 
public DateTime YourDate { get; set; } 

Hier ist, was es in der Ansicht tun sollten:

Sie
@Html.EditorFor(model => model.YourDate, new { @class = "date" }) 

Das sollte ein Datum in das Format dd formatiert out/MM/JJJJ. diese

+0

vielleicht meinst du '[DisplayFormat (DataFormatString =" {0: TT/MM/JJJJ} ")]'? –

+0

Ja, mein Schlechter. Das tut mir leid. Direkt aus meinem Code kopiert und nicht bemerkt, dass Sie ein etwas anderes Format hatten. Das tut mir leid. Hat das schon funktioniert? – IyaTaisho

+0

Und wussten Sie, dass EditorFor nicht htmlAttribues Argument hat! –

35

Das einzige Format, das ich auch an der Arbeit bekommen haben ist:

@Html.TextBoxFor(m => m.Birthdate, "{0:MM/dd/yyyy}") 
+2

Vergessen Sie nicht, dass dies nur mit MVC4 oder höher funktioniert. –

+1

** FYI **: [Viele Browser] (https://weblog.west-wind.com/posts/2012/nov/08/html5-input-typedate-formatting-issues) und die ['input [type = date] 'spec] (https://www.w3.org/TR/html-markup/input.date.html) benötigt das Format" "{0: yyyy-MM-dd}". – KyleMit

6

Die Syntax ist: @ Html.TextBoxFor (Ausdruck, String-Format, Objekt Htmlattributes)

zB:

@Html.TextBoxFor(x => x.DateUtc, "{0:yyyy-MM-dd HH:mm:ss}", 
      new { @class = "form-control", placeholder = "Enter Title", id="myDate"}) 
2

Dieses Format wird Ihnen helfen:

@Html.TextBoxFor(Model => Model._facadeLowdown.DoB, new { @value= Model._facadeLowdown.DoB.HasValue ? Model._facadeLowdown.DoB.Value.ToString("MM/dd/yyyy") : "" 
} 
0

Die Anzeige hängt von der Kultur ab. Und während in den meisten Fällen alle anderen Antworten stimmen, hat es bei mir nicht funktioniert. Culture-Problem verursacht auch andere Probleme mit jQuery Datepicker, wenn angefügt.

Wenn Sie das Format Flucht/in der folgenden Weise erzwingen wollen:

@Html.TextBoxFor(model => model.YourDate, "{0:MM\\/dd\\/yyyy}") 

Wenn nicht für mich entkommen zeigen es 08-01-2010 vs. erwartet 08/01/2010.

Auch wenn nicht maskiert jQuery datepicker wählt verschiedene defaultDate, in meiner Instanz war es May 10, 2012.

Verwandte Themen