i have problem with view for dropdown menu. Here is my code. Model:
public class student
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int StudentId { get; set; }
[Display(Name = "Šifra ispitanika:")]
[Range(1, 9999)]
public int StudentNumber { get; set; }
[Display(Name = "Datum rođenja ispitanika:")]
[DataType(DataType.Date)]
public DateTime DateBirth { get; set; }
[Display(Name = "Mjesto rođenja ispitanika:")]
public string PlaceBirth { get; set; }
[Display(Name = "Datum testiranja ispitanika:")]
[DataType(DataType.Date)]
public DateTime TestDate { get; set; }
[Display(Name = "Godina rođenja majke:")]
[Range(1900, 2000)]
public int MumDate { get; set; }
[Display(Name = "Godina rođenja oca:")]
[Range(1900, 2000)]
public int DadDate { get; set; }
[Display(Name = "Dropdown_test:")]
public string MumSport { get; set; }
[NotMapped]
public List<SelectListItem> MumSports { set; get; }
}
Controller:
public async Task<IActionResult> Create([Bind("StudentId,StudentNumber,DateBirth,PlaceBirth,TestDate,MumDate,DadDate,MumSports")] student student)
{
if (ModelState.IsValid)
{
var MumSports = new student
{
MumSports = new List<SelectListItem>
{
new SelectListItem { Text = "nikako", Value = "1" },
new SelectListItem { Text = "rekreativno", Value = "2" },
new SelectListItem { Text = "amaterski", Value = "3" },
new SelectListItem { Text = "profesionalno", Value = "4" }
}
};
_context.Add(student);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(student);
}
View:
<select asp-for="StudentId" asp-items="@(ViewBag.MumSports)">
<option>Please select one</option>
</select>
</div>
Problem is about View, when i run my app, it shows me the dropdown but without data I created in the controller. I think that this is not good way for creating View. Any idea ??
one thing which you need to make sure of when you populate any content on a page with server derived information is that you pass it down correctly.
From your view, i can see that you are looking for a ViewBag.MumSport. So, that means that in the "get" request of your action, you wil need to populate ViewBag.MumSport with the related values.
The controller youve written also looks a bit confusing... we typically expect to have a "get (verb)" controller which you use to set the page up. We usually do things like create the values for the drop down list and populate anything you need for the page. Usually, no saving of data happens here. However, it appears as though you are saving the drop down list to a database?
I believe you might want something like this:
public IActionResult Create()
{
ViewBag.MumSport = new List<SelectListItem>
{
new SelectListItem { Text = "nikako", Value = "1" },
new SelectListItem { Text = "rekreativno", Value = "2" },
new SelectListItem { Text = "amaterski", Value = "3" },
new SelectListItem { Text = "profesionalno", Value = "4" }
}
return View();
}
[HttpPost]
public IActionResult Create(view model goes in here)
{
Business logic goes here
}
This should mean that your drop down list gets populated but it also shows a distinction between the post and get methods. Post is typically where you would create or save inforamtion to a store (database).
Hope this helps
UPDATE instead of having all the properties as part of the signature for the post method, you can try using the model you have created:
public async Task<IActionResult> Create(student viewModel)
You then need to make sure your input items on the form have names which match up with the viewmodel (student):
<select asp-for="StudentId" asp-items="@(ViewBag.MumSports)">
if you do this, your properties should automatically bind to the viewModel when you hit the controller. Another benefit of doing it this way is that ModelState.IsValid will then look at all the attributes within that class and perform validation against them when you submit the form. This is particularly useful when setting fields as required using the [Required] attribute