Search code examples
asp.net-mvcasp.net-mvc-2extension-methodsstrong-typingnhlambdaextensions

ASP.Net MVC - Helper lambdaexpression post action


I created this helper method using a lambdaexpression to used strongly type helper in a view

Helper


        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?


Solution

  • 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.