manage date fields when are int in the db
Hi in my db there is a field "date_start" type integer.
This is the part of form for this field
<div class="editor-label">
<%: Html.LabelFor(model => model.date_start) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.date_start, new { @class = "calendar" })%>
<%: Html.ValidationMessageFor(model => model.date_开发者_StackOverflow中文版start) %>
</div>
In the form i want that the field's format is date type. and memorize it in int type after calling a my function for the convertion. How can i manage it?
thanks
Okay. I'm assuming you have a Linq-generated class, which we'll call LGC, and in that class is a property of type int called date_start.
To start, wrap LGC in a another class:
public class LGCPageForm
{
public LGC BaseModel { get; set; }
public DateTime date_start_as_date { get; set; }
}
Set the LGCPageForm class to be your Page's Model class. That'll require some refactoring, but I think it's your best option. Now, in your form, you'll have something like:
<div class="editor-label">
<%: Html.LabelFor(model => model.date_start_as_date) %>
<%: Html.TextBoxFor(model => model.date_start_as_date, new { @class = "calendar" })%>
<%: Html.ValidationMessageFor(model => model.date_start_as_date) %>
</div>
Then, capturing your postback. You'll definitely want to implement validation via DataAnnotations, but this should get you started in the right direction:
[HttpPost]
public ActionResult SubmitWeirdDate(LGCPageForm form)
{
//I'm not sure if this is the conversion you want?
form.LGC.date_start = form.LGC.date_start_as_date.Ticks;
}
There's the UNIX timestamp for this, which is basically an integer (and can be stored as such). I have no clue how to implement this in ASP.NET though. Check if there's a class named TimeStamp which can take an integer upon construct (or later on), then look for a form element that takes a TimeStamp. That said, I haven't got any experience at all in ASP.NET.
精彩评论