0

Im creating a helper class to collect a users date of birth. I don't want to use a date picker as I think they are a bit of a pain to use for dob.

I have created this so far

public static DateText DateTextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> ex)
    {
        htmlHelper.ViewContext.Writer.Write(
          @"<div class=""date-control"">" +
          @"<input type='text' name='Day' id='Day' maxlength='2'/>" +
          @"<input type='text' name='Month' id='Month' maxlength='2'/>" +
          @"<input type='text' name='Year' id='Year' maxlength='4'/>" +
          @"<span>e.g. DD-MM-YYYY (31-03-1980)</span>" +
          @"<input type='hidden' name='Date' id='Date'/>" +
          @"</div>"

       );

        return new DateText(htmlHelper.ViewContext);
    }

I was going to create a script to get the data from the 3 textboxes and populate the hidden field ready for the http post.

So lets say I create my control like

    @Html.DateTextFor(model => model.DOB)

What do I need to modify in my helper to bind the hidden field to the model so when on postback I have the data I want?

Also when my page renders I am getting 'My.Helpers.DateText' text been added into the page, what is causing this?

Thanks for your help

1 Answer 1

0

Cliet side, you would use JavaScript and intercept the post, populate the field and continue. Server side alternative would be combining the fields in your viewmodel, DOB read only, or you could also inherit from defaultModelBimder and handle the combining there (seems like overkill). I'm mobile so I apologize for not adding more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.