简体   繁体   English

选择列表未从ViewModel ASP.NET填充

[英]Select List not populating from ViewModel ASP.NET

The select list in one of my views is not populating from the View Model and I can't figure it out. 我的其中一个视图中的选择列表未从视图模型中填充,因此我无法弄清楚。 The other parts of the view model behave as expected. 视图模型的其他部分的行为均符合预期。 The view is a partial view, if that matters. 如果重要的话,该视图是局部视图。

Here is the code for the view: 这是该视图的代码:

 @model Workout_Tracker.ViewModels.AddExerciseViewModel
        <form >
           <div class="individualContainer">
                <label asp-for="ExerciseTypeID"></label>
                <select asp-for="ExerciseTypeID" asp-items="Model.ExercisesTypes">
                </select>
                <label asp-for="Reps"></label>
                <input type="number" asp-for="Reps" />
               </div>
            </form>

Here is the View Model: 这是视图模型:

public class AddExerciseViewModel
{
    [Display(Name ="Weight")]
    [Required(ErrorMessage="You must enter a weight.")]
    public int Weight { get; set; }

    public int Reps { get; set; }

    //this sets will be used to create the sets in the Exercise_Sets table
    public int Sets { get; set; }

    public int WorkoutID { get; set; }

    public int ExerciseTypeID { get; set; }

    public List<SelectListItem> ExercisesTypes { get; set; }

    public AddExerciseViewModel(IEnumerable<ExerciseType> exercises) {

        ExercisesTypes = new List<SelectListItem>();
        foreach (ExerciseType ex in exercises)
        {
            ExercisesTypes.Add(new SelectListItem
            {
                Value = ex.ID.ToString(),
                Text = ex.Name                  
            });
        }
    }

    public AddExerciseViewModel() {

    }
}

Here is the controller: 这是控制器:

  public IActionResult WorkoutSelector() {
        IList<ExerciseType> exerciseTypes = context.ExerciseTypes.ToList();

        AddExerciseViewModel addExerciseViewModel1 = new AddExerciseViewModel(exerciseTypes);

        return PartialView("_WorkoutSelector", addExerciseViewModel1);
    }

Here is the ExerciseType class: 这是ExerciseType类:

 public class ExerciseType
{
    public int ID { get; set; }

    public string Name { get; set; }

}

I don't normally create ActionResult methods in my controllers for the purpose of rendering partials, so I can't directly speak to extenuating circumstances surrounding your MVC pipeline. 我通常不会在控制器中创建ActionResult方法来渲染部分,所以我不能直接谈谈围绕MVC管道的情况。

Given a traditional ASP.NET MVC project, however, I tend to render my partials not by invoking actions, but as includes within my views using ViewBag as a reliable alternative for supplementing additional data to any view/partial in context as necessary. 但是,考虑到传统的ASP.NET MVC项目,我倾向于不通过调用动作来呈现我的局部视图,而是通过使用ViewBag作为可靠的替代方法在视图中包含局部视图,以便根据需要将附加数据补充到任何视图/局部视图中。

~/Views/Workout/Welcome.cshtml 〜/ Views / Workout / Welcome.cshtml

<div>
  <h2>Workout</h2>
  <p>Select your workout below:</p>

  @Html.Partial("~/Views/Shared/_WorkoutSelector.cshtml", ViewBag.MyExerciseViewModel);
</div>

Then in your controller, just load whatever is you needed in your main action and get rid of the partial action: 然后在您的控制器中,只需加载主要操作中所需的任何内容,并摆脱部分操作:

public class WorkoutController : Controller {
  ...

  public ActionResult Welcome() {
    ViewBag.MyExerciseViewModel = new AddExerciseViewModel(context.ExerciseTypes.ToList());

    return View();
  }
}

By the way, are you disposing that DbContext properly? 顺便说一句,您是否正确配置了DbContext :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM