MVC 4 DatePicker的所选值未设置为模型 [英] MVC 4 DatePicker's selected value not getting set to model
问题描述
我的日期选择器在这里
@ Html.TextBoxFor(model => model.CallDetailModel.CallDate,new {id = callDate})
我的JavaScript在这里
@section脚本{
< script>
$(#callDate)。datepicker({
changeMonth:true,
changeYear:true
});
< / script>
}
我已将主题和界面添加到 _Layout
关闭页脚后和身体标签结束之前
@ 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;组; }
一旦我点击提交按钮,我无法获取选定的日期值
它显示我的空值。
我的创建动作
在这里
[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 = models.CallDetailModel.ContactDetailId;
obj.Id = model.CallDetailModel.ContactDetailId;
obj.InquirySerialNo = model.CallDetailModel.InquirySerialNo;
obj.Priority = model.CallDetailModel.Priority;
obj.ProjectName = model.CallDetailModel.ProjectName;
obj.PropertySerialNo = models.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);
//创建一个频道。
IBuilderTrackerServices wcfClientProperty = myChannelFactory.CreateChannel();
var modelResult = wcfClientProperty.CreateCall(obj);
((IClientChannel)wcfClientProperty).Close();
if(modelResult)
返回RedirectToAction(〜/ Views / CRM / Call / Index.cshtml);
}
return View(obj);
}
什么是错误我不知道..?
谢谢。
Omg什么代码...我不知道你想做什么,但绑定看起来正确,我不知道为什么你的日期是空的。
解决问题:
- 确保输入文本框位于表单标签中。
-
将表单收集参数添加到操作中,并尝试以调试模式进行检查。
public ActionResult Create(CallModels model,FormCollection form)
-
确保为DateTime定义的无定制模型绑定。 p>
- 确保日期格式正确。尝试将字段类型从DateTime更改为字符串并检查结果。
但是你的代码还有另外一个问题。尝试做一些像
[HttpPost]
public ActionResult Create(CallModels model)
{
if(ModelState.IsValid)//仅验证(TryValidateModel(model.CallDetailModel))
{
myChannelFactory = new ChannelFactory< IBuilderTrackerServices>(myBinding,myEndpoint);
//创建一个频道。
IBuilderTrackerServices wcfClientProperty = myChannelFactory.CreateChannel();
var modelResult = wcfClientProperty.CreateCall(model.CallDetailModel); //不要创建新的细节对象,只需从模型中获取。
((IClientChannel)wcfClientProperty).Close();
if(modelResult)
返回RedirectToAction(Index); //重定向到anction - 不查看。
}
return View(model); // !!!!!将模型返回到同一页面,而不是obj
}
I have DatePicker I am able to selecte the value of DatePicker and I was set to TextBox but I am not able to get into model it show me null.?
my date picker is here
@Html.TextBoxFor(model => model.CallDetailModel.CallDate, new { id = "callDate" })
my JavaScript here
@section Scripts {
<script>
$("#callDate").datepicker({
changeMonth: true,
changeYear: true
});
</script>
}
I have added theme and UI into _Layout
after closing of footer and before end of body tag
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/themes/base/css")
@Scripts.Render("~/bundles/jquerymenu")
@RenderSection("scripts", required: false)
my CallDate property like this
public DateTime? CallDate { get; set; }
once I click on submit button that time I am not able to get the selected value of date it show me null value.
My Create Action
is here
[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);
}
what is going to wrong I don't know..? thank you.
Omg what a code... I dont know what you trying to do but the binding looks right and i have no idea why your date is null.
To solve problem:
- Ensure that the input text box are in form tag.
Add the form collection parameter to your action and try inspect it in debug mode.
public ActionResult Create(CallModels model, FormCollection form)
Ensure that the no custom modelbinder defined for DateTime.
- Ensure that the date has correct format.Try to change field type from DateTime to string and inspect result.
But you have many another problem with your code. try to do something like
[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
}
这篇关于MVC 4 DatePicker的所选值未设置为模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!