ASP.Net MVC - Helper lambdaexpression post action
I created this helper method using a lambdaexpression to used strongly type helper in a view
Helper
开发者_如何学C public static string DateFor<TModel, TDate>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TDate>> expression)
{
ModelMetadata data = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
StringBuilder sb = new StringBuilder();
...
//code that creates three dropdownlist (day, month and year)
...
}
view
<%= Html.LabelFor(model => model.DataNascita) %>
Controller
[HttpPost]
public ActionResult Edit(int id, Account MyAccount)
{
...
return View(...);
}
my problem is that the MyAccount.DataNascita is not set with the value i choose in the Edit form (date's value minimun.. ex. 1900/01/01).
how to bind it in a Edit post action?
I think what you are after is a custom ModelBinder that will parse the incoming posted data and convert it into a DateTime for your model.
In your view, you say that you are outputting a Label for something, but your question concerns why the value isn't kept on posting.
I think you need to, at least, say Html.EditorFor(...)
, but you probably want to use the helper method you wrote:
<%: Html.DateFor(model => model.DataNascita) %>
However, it is impossible to say exactly what is going on here without knowing the data type of the DataNascita
property on the model.
I suggest you use Jquery Datepicker.
You can customize it with Jquery Themeroller.
精彩评论