[英]ASP.NET MVC - Provide additional data to dynamically created ViewModel
我有一個視圖,允許用戶動態添加項目(在本例中為“電影”)。 每部電影都有一個“評級”,應該可以從下拉列表中選擇。 以前,這來自枚舉,並且效果很好(這似乎在下面注釋掉了)。 但是,“評級”列表最近已移至配置文件中。
代碼如下所示:
創建.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="removeMovie_@Model.Uid" class="btn" value="Remove" />
</td>
</tr>
電影控制器.cs
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddMovie([Bind("Id,Movie")] ShelfAddViewModel shelf)
{
shelf.Movies.Add(new MovieAddViewModel());
return PartialView("_Movie", shelf);
}
電影添加視圖模型.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; }
}
新的評級列表來自一個 JSON 文件,加載方式如下:
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();
我的問題是,如何將這個 List 放到 ViewModel 上? 我無法將它添加到構造函數中,因為 ASP 不知道如何動態創建對象,除非構造函數沒有參數。 我不能在創建 ViewModel 時只更新 RatingList 屬性,因為當我添加另一個電影時它會消失。 我也不覺得我應該直接從我的 ViewModel 訪問設置。 這里最好的方法是什么?
(請注意:我已經修改了我的代碼,以便讓其他有同樣問題的人更容易理解問題 - 對任何錯誤表示歉意。請告知我是否可以更好地表達我的問題標題)
有很多方法可以做到這一點。 其中之一只是將其加載到控制器內部。
我很難從你的回答中理解你真正使用的是什么控制器或動作,所以我會給你一個提示,你可以根據你的代碼進行調整
public class MyController : Controller
{
private readonly List<SelectListItem> _movieStandardSelectList ;
public MyController( IConfiguration configuration)
{
var movieStandards = configuration.GetSection("Standards:MovieStandards").Get<List<string>>();
_movieStandardSelectList = movieStandards.Select(x=> new SelectListItem() { Text = x, Value = x }).ToList();
}
public ActionResut MyAction()
{
var model = new MovieAddViewModel
{
RatingList= _movieStandardSelectList;
.... another your code
}
return View(model);
}
我通常使用的另一種方法是創建一個全局靜態變量
public static List<SelectListItem> MovieStandardSelectList {get; set;};
並將您可以在構造函數中看到的代碼放入啟動文件中,並將列表分配給該全局變量。 但我不把詳細代碼放在這里,因為我怕它會遇到太多的批評。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.