Hello,
This is my first try at MVC Core.
I have a table with datetime data type. I have defined the model for this table.
When I declare the property in the model as datetime, the model does not show any records.
When I declare the property as string, the model shows data, but then then the context returns invalid cast from datetime to string error.
Kindly guide.
Thanks.
Shailesh BistPosted Dec 9, 2022, 11:44 AM
Hi Vishal,
Thanks for the response.
My context code is as below.
public class ApplicationDbContext : DbContext options) : base(options)
{
public ApplicationDbContext(DbContextOptions
{
}
public DbSet RAJ_AAJ_KA_SHABDH { get; set; } RAJ_DAILY_SHABD { get; set; }
public DbSet
}
My controller code is as below.
public async Task Index()
{
List AAJ =await _db.RAJ_AAJ_KA_SHABDH.ToListAsync();
HomePageVM VM = new HomePageVM()
{
DailyHindi = GetHindiText(_db.RAJ_AAJ_KA_SHABDH.Find(_db.RAJ_DAILY_SHABD.FirstOrDefault().Sr_No).Hindi_Description.ToString()),
DailyEnglish = _db.RAJ_AAJ_KA_SHABDH.Find(_db.RAJ_DAILY_SHABD.FirstOrDefault().Sr_No).English_Description.ToString(),
};
return View(VM);
}
When I declare the property as datetime, the code highlighted in red gives errror because the dbset is null.
When I declare the property as string, then I get string to datetime conversion error in the code highlighted in yellow.
Regards
Vishal JoshiPosted Dec 9, 2022, 9:14 AM
Hello Shailesh
Please try the below code to work with the date in MVC.
Sachin SinghPosted Dec 9, 2022, 8:17 AM
On view page do this
Pankajkumar PatelPosted Dec 9, 2022, 6:10 AM
Display DateTime value in dd/mm/yyyy format in Asp.NET MVC
Hope this will help you!
Shailesh BistPosted Dec 9, 2022, 5:41 AM
Hi Pankaj,
Thanks for the information. I checked the link. This is when we post data.
In my case, I am not posting anything. This is just a view page. Will it work for this view also?
Thanks.
Pankajkumar PatelPosted Dec 9, 2022, 5:25 AM
Hi Shailesh Bist,
This is one of the good link with practical example:
Hope this will help you!