MVC, submit text value into date property

Not actually sure how to phrase my question. I'm using jqgrid in most of my screens (not sure if its relevant info in this case), Iv'e got two datetime pickers on the add/edit modal. I've been using [this][1] Date Time picker component, which worked well except i find that people aren't fans of using sliders to capture time, esp if its something that needs to get entered frequently. Along came the [will_pickdate][2] component which although its super luminous :P, seemed to answer my end users prayers, (my other option was to try and write my own component but i'll give it a skip for now) My problem comes in when i try and save. the will_pickdate component seems to be submitting its date time values as text, or its not mapping correctly when i call the TryUpdateModel method. **Client Side Code** function CreateDateTimePicker(elem, ShowOn, OnClose) { setTimeout(function () { //code that works $(elem).datetimepicker({ dateFormat: 'yy/mm/dd', timeFormat: 'hh:mm', showOn: ShowOn, buttonImageOnly: true, buttonImage: "/Images/date_time.jpg", changeYear: true, changeMonth: true, showButtonPanel: true, showWeek: true, onClose: function (dateText, inst) { if (OnClose != null) OnClose(dateText, inst); $(this).focus(); } }).attr('size', '16').next('img.ui-datepicker-trigger') .attr("tabIndex", "-1").css({ 'cursor': 'pointer', 'vertical-align': 'middle', 'padding-left': '3px', 'padding-bottom': '4px' }); //new code that sort of works.. eg component renders fine, but fails server side //$(elem).will_pickdate({ // timePicker: true, // format: 'Y/m/d H:i', // inputOutputFormat: 'Y/m/d H:i', // militaryTime: true, // allowEmpty: true, // startView:'day', // onSelect: function (date) { // if (OnClose != null) // OnClose(); // $(this).focus(); // // $('#' + display[0].id).val(new Date($(elem).val())); // // $('#' + display[0].id+ '_display').val(new Date($(elem).val())); // // alert($('#' + display[0].id).val()); // } //}); }, 100);} **My add method.** public ActionResult Edit(Down_Time_Capture viewModel, FormCollection formCollection) { Down_Time_CaptureRepository repository = new Down_Time_CaptureRepository(); try { if (!ModelState.IsValid) return ReturnValidationFailure(ViewData.ModelState.Values); int item_id = Convert.ToInt32(formCollection["id"]); var model = repository.Where(o => o.DTCP_ID == item_id).SingleOrDefault(); if (model == null) { //append any error code to allow jqgrid modal to handle error display Response.StatusCode = 400; return Json("Record not found.", JsonRequestBehavior.AllowGet); } ====> //code fails here, model tries to get updated but dies if (TryUpdateModel(model, formCollection.ToValueProvider())) { repository.Edit(model, User.Identity.Name); repository.Save(); return Json("Updated successfully.", JsonRequestBehavior.AllowGet); } else { return ReturnValidationFailure(ViewData.ModelState.Values); } } catch (Exception ex) { ... } } What Iv'e noticed is that the view model is valid and contains the values in datetime format, but when i try update my model from the db, it fails with the following message. *The parameter conversion from type 'System.String' to type '..Portal.Models.Down_Time_Capture' failed because no type converter can convert between these types.* I've tried converting the value to a date format in my javascript/jquery, and append it to my Date Input field... but it still submits it as string I'll provide any other information if its needed, but this is a strange one :/ **UPDATE:** My view only contains the html for the jqgrid component. I've added a jsfiddle link below. [Link To JsFiddle - Includes code for both datepickers][3] [1]: http://trentrichardson.com/examples/timepicker/ [2]: http://zenapsis.github.com/will_pickdate/ [3]: http://jsfiddle.net/Yenros/vawtN/7/

以上就是MVC, submit text value into date property的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » jQuery 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏