funktionierts habe ich ein Ansichtsmodell mit einer Eigenschaft wie folgt:MVC2 Displayformat Attribut nicht nach dem Postback
[DisplayName("As Configured On:")]
[DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
public DateTime ConfigDate { get; set; }
Das Formular, das die ConfigDate anzeigt, ist wie folgt:
<%= Html.EditorFor(Model => Model.ConfigDate)%>
Wenn der Index Aktion kommt zurück, alles sieht korrekt formatiert aus, dh die <input>
Box hat den Datumswert wie 12.12.2001. Wenn das Formular gebucht wird, ist das Ergebnis, das zurückkommt, als ob das DisplayFormat
Attribut nicht angewendet wird.
EDIT: Weitere Informationen angefordert wurde: hier ist der Code en toto:
das Suchformular
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Config.Web.Models.AirplanesViewModel>" %>
<% using (Html.BeginForm("Details", "Airplanes", FormMethod.Post, new { id = "SearchForm" })) { %>
<%= Html.LabelFor(model => model.ConfigDate) %>
<%= Html.EditorFor(Model => Model.ConfigDate)%>
<input id="searchButton" type="submit" value="Search" />
<% } %>
Die AirplanesViewModel
public class AirplanesViewModel
{
[DisplayName("As Configured On:")]
[DisplayFormat(DataFormatString="{0:d}", ApplyFormatInEditMode=true)]
public DateTime ConfigDate { get; set; }
}
}
The Controller
[HttpGet]
public ActionResult Index()
{
AirplanesViewModel avm = new AirplanesViewModel
{
ConfigDate = DateTime.Now
};
return View(avm);
}
[HttpPost]
[ActionName("Details")]
public ActionResult Details_Post(AirplanesViewModel avm)
{
return RedirectToAction("Details", avm);
}
[HttpGet]
public ActionResult Details(AirplanesViewModel avm)
{
int page = 0;
int pageSize = 10;
if (!ModelState.IsValid)
{
avm.Airplanes = new PaginatedList<Airplane>();
return View(avm);
}
try
{
Query q = new Query(avm.Query);
PaginatedList<Airplane> paginatedPlanes = new PaginatedList<Airplane>(repo.ByQuery(q), page, pageSize);
avm.Airplanes = paginatedPlanes;
return View(avm);
}
catch (Exception)
{
// Should log exception
avm.Airplanes = new PaginatedList<Airplane>();
return View(avm);
}
}
Weitere Informationen
Es hat etwas mit der Umleitung auf die GET-Aktion zu tun. Wenn ich die POST-Aktion herausnehme und das GET-Attribut entferne (so verwenden sowohl GET als auch POST die Details() -Aktion), verschwindet das Problem - aber das wird auch meine hübschen URLs los, wenn das Formular abgeschickt wird (und verursacht das lästige " Bist du sicher? "Popup bei Aktualisierung. Seltsamerweise ist das einzige Problem der Verlust der Formatierung in diesem Feld. Alles andere ist in Ordnung.
Bitte zeigen Sie Controller-Aktionen (vorzugsweise sowohl GET und POST), Ihr gesamtes Modell und Ansicht, so dass das Problem eindeutig angegeben ist. Im Moment ist es nur ein bisschen raten hier und ich hasse es zu erraten. Ich bevorzuge es, Probleme zu lösen. –