1

I am trying to bind the date selected by the DatePicker to the model that is strongly typed to my view. The property inside the model (NewEditDataTypeModel) that I'm trying to bind to is "codeTypeNewEditModel.EffectStartDate". The datatype is DateTime.

My view is strong typed as follows:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<TMS.MVC.BusinessSystemsSupport.Models.NewEditDataTypeModel>" %>

** My DatePicker is setup as follows:**

Effective Start Date: <input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="Model_codeTypeNewEditModel_EffectStartDate"/>


<script type="text/javascript">
$(document).ready(function () {
$("#Model_codeTypeNewEditModel_EffectStartDate").datepicker({
  showOn: 'button',
  buttonImage: '/Content/images/calendar.gif',
  duration: 0 
});
});

My action method is as follows:

    [HttpPost]
    public ActionResult DataTypeNewEdit(NewEditDataTypeModel newEditDataTypeModel)
    {  etc

The newEditDataTypeModel model doesn't populate the effected start date from the DatePicker. Does anyone know what I'm doing wrong? I believe using the underscores is the correct thing to do.

1 Answer 1

3

You don't need to have Model as a prefix for the input name.

The name of the input must use dot to indicate a property. Not underscore

So if you will replace:

<input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="Model_codeTypeNewEditModel_EffectStartDate"/>

with

<input type="text" id="Model_codeTypeNewEditModel_EffectStartDate" name="newEditDataTypeModel.EffectStartDate"/>

It should work for you.

Here, newEditDataTypeModel should be equal to the parameter on the action.

But I would just take advantage of strongly type helpers and use Html.TextBoxFor.

Sign up to request clarification or add additional context in comments.

2 Comments

'First of all, you say that your view is strongly type, but according to the code it is not.' this is what I was about to post but then I clicked edit and saw that because he hadn't used code formatting the content after ...ViewUserControl was getting trimmed out due to the <. Anyway, this is the correct answer.
I updated the answer to reflect that. Also added another option for you at the end. Mind to accept the answer then?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.