I need populate on MVC form View
the TextBox below
@Html.LabelFor(m => m.tDate)
@Html.TextBoxFor(m => m.tDate, "{0:dd/MM/yyyy}", new { @class = "Mytextarea2" })
@Html.ValidationMessageFor(m => m.tDate, "", new { @class = "text-danger" })
Whit value extract from controller
(using MySql database) when I have planned this code
public DateTime JsonDateTimeToNormal(string jsonDateTime)
{
jsonDateTime= @"""" + jsonDateTime + @"""";
return Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime>(jsonDateTime);
}
...
model.tDate = JsonDateTimeToNormal(GetDateHourById(value).ToString());
...
private static string GetDateHourById(string value)
{
string sql;
string constr = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
sql = @String.Format(" SELECT ");
sql += String.Format(" tDate ");
sql += String.Format(" FROM `dotable` ");
sql += String.Format(" WHERE tID = @Id;");
using (MySqlCommand cmd = new MySqlCommand(sql))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@Id", value);
con.Open();
string name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return name;
}
}
}
The value memorized on MySql database is
2020-03-19
But the TextBox
is populed with this value
/Date(1605481200000)/
On the model
[Column(TypeName = "date")]
[Display(Name = "Your date")]
[Required]
[DataType(DataType.Date)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
public DateTime? tDate { get; set; }
Help me to do it.
Debug VS 2109
Solution
const d = new Date(response.tDate.match(/d+/)[0] * 1);
const formattedDate = d.getFullYear()+'-'+("0"+(d.getMonth()+1)).slice(-2)+'-'+("0"+d.getDate()).slice(-2)
$('#tDate').val( formattedDate );
question from:
https://stackoverflow.com/questions/65846721/populated-on-mvc-form-view-the-textbox 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…