0

I am working in Ajax request to send data in Controller by using Jquery, all the fields are got fine @controller, but only one field Date("AffectedDate") is not coming.
can anybody tell me where 'm wrong?

JQuery Code:

function saveData() {
    var data = $("#editForm").serialize();
    $.ajax({
        type: 'GET',
        url: '/Controller/SaveTaxRate/',
        data: data,
        success: function (result) {
            alert('success');
        },
        error: function (jqXHR) {
            alert('failure');                
        }
    })
}

.Cs Model:

public class TaxRateModel
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }
    public double? Taxes { get; set; }
    public float? AffectedTaxRate { get; set; }
    public Nullable<System.DateTime> AffectedDate { get; set; }
}

Controller Code:

public bool SaveTaxRate(TaxRateModel taxRateModel)
{
     // My Code here..
}

HTML Code:

<div class="row-fluid">
  <div class="span12">
      <div class="span3">
          <p><strong>Country: </strong></p>
      </div>
      <div class="span5">
          @Html.TextBoxFor(Model => Model.CountryName, new { @placeholder = "Change the Country Name" })
      </div>
  </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <div class="span3">
            <p><strong>Tax-Rate: </strong></p>
        </div>
        <div class="span5">
            @Html.TextBoxFor(Model => Model.Taxes, new { @placeholder = "Chnage the Tax-Rate", @id = "TaxRate" })
        </div>
    </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <div class="span3">
            <p><strong>Affected Tax-Rate: </strong></p>
        </div>
        <div class="span5">
            @Html.TextBoxFor(Model => Model.AffectedTaxRate, new { @placeholder = "Change the TaxRates" })
        </div>
    </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <div class="span3">
            <p><strong>Affected Date: </strong></p>
        </div>
        <div class="span5">
            @Html.TextBoxFor(model => model.AffectedDate, "{0:dd/MM/yyyy}", new { @placeholder = "Change the Affected Date" })                                        
        </div>
    </div>
</div>
Fabio
  • 11,892
  • 1
  • 25
  • 41
Smit Patel
  • 2,992
  • 1
  • 26
  • 44

2 Answers2

2

The parsing of DateTime happens culture aware for Formvalues.

Check what you have in CultureInfo.CurrentCulture

Here is the code in the Framework:

if (rawValue == null && request.Form != null)
{
    culture = CultureInfo.CurrentCulture;
    rawValue = request.Form.GetValues(name);
}

http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization

If you need to change this behaviour you can do so by providing your own Modelbinder:

ModelBinders.Binders.Add(typeof(DateTime), new MyModelBinder());

also in http://weblogs.asp.net/melvynharbour/mvc-modelbinder-and-localization .

EDIT

because the request is send via get and the parameter is passed in the querystring the date is parsed using invariant culture.

this part of the framework is used

 CultureInfo culture = CultureInfo.InvariantCulture;

 if (request.QueryString != null)
 {
     rawValue = request.QueryString.GetValues(name);
 }

I guess you should try ajays answer and use post.

Mathias F
  • 15,906
  • 22
  • 89
  • 159
2

Try this. I used same.

var data = new FormData($("#editForm").get(0));
$.ajax({
    type: "POST",
    url: '/Controller/SaveTaxRate/',
    data: data,
    dataType: "json",
    contentType: false,
    processData: false,
    success: function (result) {
        alert('success');
    },
    error: function (jqXHR) {
        alert('failure');                
    }
})

instead of

var data = $("#editForm").serialize();
Ajay
  • 6,418
  • 18
  • 79
  • 130