I have a view that allows the user to dynamically add items (In this case, "movies"). Each movie has a "Rating" that should be selectable from a drop-down list. Previously, this came from an enum, and this worked fine (This appears commented out below). However, the "Rating" list has recently been moved to a configuration file.
The code looks like so:
Create.cshmtl
@model MyProject.Models.ViewModels.ShelfAddViewModel
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Movie[0].Rating)
</th>
<th></th>
</tr>
</thead>
<tbody id="movieContainer">
@Html.EditorFor(model => model.Movie)
</tbody>
<tfoot id="item-list">
<tr>
<td>
<input type="button" id="btnAdd" class="btn" value="Add" />
</td>
</tr>
</tfoot>
...
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script type="text/javascript">
$("#btnAdd").on('click', function () {
$.ajax({
async: true,
data: $('#form').serialize(),
type: "POST",
url: '/Movies/AddMovie',
success: function (partialView) {
$('#movieContainer').html(partialView);
}
});
});
MovieAddViewModel.cshtml
@model MyProject.Models.ViewModels.MovieAddViewModel
<tr>
<td>
<input asp-for="Id" class="form-control" />
</td>
<td>
<select asp-for="Rating" asp-items="Model.RatingList" class="form-control"></select>
@*<select asp-for="Rating" asp-items="Html.GetEnumSelectList<RatingType>()" class="form-control"></select>*@
</td>
<td>
<input type="button" onclick="Remove(this)" id="[email protected]" class="btn" value="Remove" />
</td>
</tr>
MovieController.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddMovie([Bind("Id,Movie")] ShelfAddViewModel shelf)
{
shelf.Movies.Add(new MovieAddViewModel());
return PartialView("_Movie", shelf);
}
MovieAddViewModel.cs
public class MovieAddViewModel
{
[Display(Name = "Movie Title")]
public string Name { get; set; }
[Display(Name = "Rating")]
public string Rating { get; set; }
// public RatingType Rating { get; set; }
public List<SelectListItem> RatingList { get; set; }
}
The new list of ratings comes from a JSON file and is loaded like so:
var standards =
_configuration.GetSection("Standards");
var movieStandards = standards.GetSection("MovieStandards").Get<List<string>>();
var movieStandardSelectList = movieStandards.Select(x=> new SelectListItem() { Text = x, Value = x }).ToList();
My question is, how do I get this List onto the ViewModel? I can't add it to the constructor as ASP doesn't know how to dynamically create objects unless the constructor has no parameters. I can't just update the RatingList property when I create the ViewModel, as this disappears when I add another Movie. I don't feel like I should be directly accessing the settings from my ViewModel either. What's the best approach here?
(Please note: I've modified my code to make it an easier to follow question for anyone else who has the same issue - apologies for any mistakes. Please advise if I can word my question title better)
There are a lot of ways to do this. One of them just load it inside of the controller.
It is hard for me to understand from your answer what controllers or actions you are really using, so I will give you a hint , and you can adapt it to your code
another way that I am usually using is to create a global static variable
and put the code that you can see in the constuctor into the startup file where assign the list to this global variable. But I don't put the detail code here since I am afraid that it will meet too many critics.