1

我有 DatePicker 我能够选择 DatePicker 的值并且我被设置为 TextBox 但我无法进入模型它显示我为空。?

我的日期选择器在这里

@Html.TextBoxFor(model => model.CallDetailModel.CallDate, new { id = "callDate" })

我的 JavaScript 在这里

@section Scripts {
    <script>
        $("#callDate").datepicker({
            changeMonth: true,
            changeYear: true
        });

</script>
}

_Layout在页脚关闭后和正文标签结束前添加了主题和 UI

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Styles.Render("~/Content/themes/base/css")
    @Scripts.Render("~/bundles/jquerymenu")
    @RenderSection("scripts", required: false)

我的 CallDate 属性是这样的

public DateTime? CallDate { get; set; }

一旦我点击提交按钮,我就无法获得选定的日期值,它会显示空值。

Create Action的在这里

    [HttpPost]
    public ActionResult Create(CallModels model)
    {
        CallDetail obj = new CallDetail();
        obj.AccompaniedBy = model.CallDetailModel.AccompaniedBy;
        obj.BudgetFrom = model.CallDetailModel.BudgetFrom;
        obj.BudgetTo = model.CallDetailModel.BudgetTo;
        obj.CallCategory = model.CallDetailModel.CallCategory;
        obj.CallDate = model.CallDetailModel.CallDate;
        obj.CallDescription = model.CallDetailModel.CallDescription;
        obj.CallDuration = model.CallDetailModel.CallDuration;
        obj.CallTime = model.CallDetailModel.CallTime;
        obj.CallType = model.CallDetailModel.CallType;
        obj.ContactDetailId = model.CallDetailModel.ContactDetailId;
        obj.Id = model.CallDetailModel.ContactDetailId;
        obj.InquirySerialNo = model.CallDetailModel.InquirySerialNo;
        obj.Priority = model.CallDetailModel.Priority;
        obj.ProjectName = model.CallDetailModel.ProjectName;
        obj.PropertySerialNo = model.CallDetailModel.PropertySerialNo;
        obj.PurposeOfCall = model.CallDetailModel.PurposeOfCall;
        obj.Reference = model.CallDetailModel.Reference;
        obj.Reminder = model.CallDetailModel.Reminder;
        obj.Result = model.CallDetailModel.Result;
        obj.Tag = model.CallDetailModel.Tag;
        obj.ContactDetail = null;
        obj.IsActive = true;

        if (ModelState.IsValid)
        {
            myChannelFactory = new ChannelFactory<IBuilderTrackerServices>(myBinding, myEndpoint);
            // Create a channel.
            IBuilderTrackerServices wcfClientProperty = myChannelFactory.CreateChannel();
            var modelResult = wcfClientProperty.CreateCall(obj);
            ((IClientChannel)wcfClientProperty).Close();
            if (modelResult)
                return RedirectToAction("~/Views/CRM/Call/Index.cshtml");
        }
        return View(obj);
    }

出了什么问题我不知道..?谢谢你。

4

1 回答 1

0

天哪,什么代码...我不知道您要做什么,但绑定看起来正确,我不知道为什么您的日期为空。

解决问题:

  1. 确保输入文本框在表单标签中。
  2. 将表单集合参数添加到您的操作并尝试在调试模式下检查它。

    public ActionResult Create(CallModels model, FormCollection form)

  3. 确保没有为 DateTime 定义自定义模型绑定器。

  4. 确保日期格式正确。尝试将字段类型从 DateTime 更改为字符串并检查结果。

但是您的代码还有许多其他问题。尝试做类似的事情

   [HttpPost]
public ActionResult Create(CallModels model)
{       
    if (ModelState.IsValid) //to validate the detail only if(TryValidateModel(model.CallDetailModel))
    {
        myChannelFactory = new ChannelFactory<IBuilderTrackerServices>(myBinding, myEndpoint);
        // Create a channel.
        IBuilderTrackerServices wcfClientProperty = myChannelFactory.CreateChannel();
        var modelResult = wcfClientProperty.CreateCall(model.CallDetailModel); //dont create the new detail object, just get it from model.
        ((IClientChannel)wcfClientProperty).Close();
        if (modelResult)
            return RedirectToAction("Index");//redirect to anction - not to view.
    }
    return View(model); //!!!!! return your model to a same page not an obj
}
于 2013-06-17T10:41:49.227 回答