I didn't understand your code very well, and I don't know AJAX
But if you need to populate some information.
You need to have a model and a view, as you have.
And you also need to pass the value to the model and return that value to the view.
I don't see you doing that.
So I would have something like this
Public ActionResult GetDateById (string value)
{
//the code that would get my info from db
//somthing like this
var dateFromDb= letSayThisTheDate;
//returning that result to my textBox with the model
return View(dateFromDb);
}
In your case I don't see where you pass the result to the view.
Member
63 Points
129 Posts
Populated on MVC form View the TextBox
Jan 23, 2021 06:59 AM|Golia|LINK
Hi all,
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 codepublic DateTime JsonDateTimeToNormal(string jsonDateTime) { jsonDateTime= @"""" + jsonDateTime + @""""; return Newtonsoft.Json.JsonConvert.DeserializeObject<DateTime>(jsonDateTime); } ... model.tDate = JsonDateTimeToNormal(GetDateById(value).ToString()); ... private static string GetDateById(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-04-09
But the output in
TextBox
isOn Ajax
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 VS2019
Member
17 Points
24 Posts
Re: Populated on MVC form View the TextBox
Jan 23, 2021 08:46 AM|Belarmino Vicenzo|LINK
I didn't understand your code very well, and I don't know AJAX
But if you need to populate some information.
You need to have a model and a view, as you have.
And you also need to pass the value to the model and return that value to the view.
I don't see you doing that.
So I would have something like this
In your case I don't see where you pass the result to the view.