I have datetime field in database, and i am using DateTime property for the date.
I am displaying date in text box in format "dd.MM.yyyy"
@Html.TextBox("V开发者_开发问答alidity", Model.Validity.ToString("dd.MM.yyyy"))
Now I have to save it to the data base, means how can I assign this value to the property so that I will be able to get data in my controller.
Thanks.
In you controller you can retrive this field using your Model or FormsCollection.. From there you can format the "." dot character to the format you are expecting.
If you pass this to SQL as a SQLDBType.DateTime, you should be fine..
As you haven't explained what problems you are encountering with this code here's an example that worked for me and allowed me to successfully retrieve the date stored in dd.MM.yyyy
format in the POST action.
Model:
public class MyViewModel
{
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
public DateTime Validity { get; set; }
}
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
Validity = DateTime.Now
});
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
View:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.EditorFor(x => x.Validity)
<input type="submit" value="OK" />
}
精彩评论