Reputation: 491
I am using telerik DatePickerFor for getting datetime value. Here is my model class:
public class Kisi
{
[Display(Name="date1")]
public DateTime date1{ get; set; }
[Display(Name = "date2")]
public DateTime date2 { get; set; }
[Display(Name = "date3")]
public DateTime date3{ get; set; }
}
Here is my view class:
//most parts ommited for brevity
@model LojmanMVC.Domain.Kisi
@{
ViewBag.Title = "KisiOlustur2";
}
<h2>KisiOlustur2</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<!-- date1-->
<div>
<div class="editor-label">
<label>date1? </label>
</div>
<div class="editor-field">
@(Html.Kendo().DatePickerFor(m=>m.date1)
.Name("dtpickerMemuriyetBaslama")
.Min(new DateTime(1900, 1, 1))
.Max(new DateTime(2099, 12, 31))
.Value(DateTime.Today)
)
<br />
<!-- Html.ValidationMessageFor(model => model.MemuriyetBaslamaTarihi)-->
</div>
</div>
<div id="tey" style="display:none">
<div class="editor-label">
@Html.LabelFor(model => model.date2)
</div>
<div class="editor-field">
@(Html.Kendo().DatePickerFor(m=>m.date2)
.Name("dtAskerlikBaslama")
.Min(new DateTime(1900, 1, 1))
.Max(new DateTime(2099, 12, 31))
.Value(DateTime.Today)
)
@Html.ValidationMessageFor(model => model.AskerlikBaslangicTarihi)
</div>
</div>
<div id="hey" style="display:none">
<div class="editor-label">
@Html.LabelFor(model => model.date3)
</div>
<div class="editor-field">
@(Html.Kendo().DatePickerFor(m=>m.AskerlikBitisTarihi)
.Name("dtAskerlikBitis")
.Min(new DateTime(1900, 1, 1))
.Max(new DateTime(2099, 12, 31))
.Value(DateTime.Today)
)
<p>
<input type="submit" value="Oluştur" />
</p>
</div>
</div>
And here is my controller :
public class KisiController : Controller
{
//
// GET: /Kisi/
public ActionResult Index()
{
return View();
}
public ActionResult KisiOlustur2()
{
Kisi kisi = new Kisi();
return View(kisi);
}
[HttpPost]
public ActionResult KisiOlustur2(Kisi sisi)
{
return View(sisi);
}
}
at sisi variable, date1,date2 and date3 is null, even though I entered them. How can I solve it? Thanks in advance.
Upvotes: 1
Views: 1139
Reputation: 1501
Firstly, one thing I would suggest adding to your model, is the following:
[DataType(DataType.Date, ErrorMessage = "Please enter a valid date.")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
[Display(Name="date1")]
public DateTime date1{ get; set; }
You can customise the format string how you like and also your error message. This will ensure what the user enters has to actually be a date in the correct format.
However, I think your issue is that you are not actually passing the date from the view to the controller, you need to give your DatePickerFor a name and then pass this name into the controller for example:
[HttpPost]
public ActionResult KisiOlustur2(Kisi sisi, DateTime dtAskerlikBitis)
{
return View(sisi);
}
Upvotes: 1