为什么在填充下拉列表时会发生此Web http异常?

时间:2016-07-03 07:49:07

标签: c# asp.net asp.net-mvc razor drop-down-menu

型号

LeagueLeagueDivision是两个模型类

public class League
{
    public int Id { get; set; }
    public string League1 { get; set; }
    public string Icon { get; set; }

    public virtual ICollection<LeagueDivision> LeagueDivisions { get; set; }
}

public class LeagueDivision
{
    public int Id { get; set; }
    public Nullable<int> LeagueId { get; set; }
    public string Name { get; set; }
    public string Icon { get; set; }

    public virtual League League { get; set; }
} 

  public class ViewModelForHostBooster
    {
        [Required(ErrorMessage = "Please enter price")]
        [Display(Name = "Price")]
        public decimal Price { get; set; }       

        [Required(ErrorMessage = "Please select a league")]
        [Display(Name = "League")]

        public int? SelectedLeague { get; set; }
        [Required(ErrorMessage = "Please select a league division")]
        [Display(Name = "League Division")]

        public int? SelectedLeagueDivision { get; set; }

        public SelectList LeagueList { get; set; }
        public SelectList LeagueDivisionList { get; set; }      


    }

控制器

IndexDropdown操作中,我只是使用模型填充视图并验证是否 验证模型然后填充视图,否则返回视图。在FetchLeagueDivision操作中,我根据传递的参数ID选择模型类的Id和Name属性。

当我运行这段代码时,有人可以指导我WebHttpException发生的原因吗?以下是异常Http Exception Image

的链接
public class DropDownController : Controller
{
    [HttpGet]
    public ActionResult IndexDropDown()
    {
        ViewModelForHostBooster model = new ViewModelForHostBooster();

        ConfigureViewModel(model);
        return View(model);
    }

    [HttpPost]
    public ActionResult IndexDropDown(ViewModelForHostBooster model)
    {
        if (!ModelState.IsValid)
        {
            ConfigureViewModel(model);
            return View(model);
        }

        // save and redirect
        return RedirectToAction("Somewhere");
    }


    private void ConfigureViewModel(ViewModelForHostBooster model)
    {
        HostBoostersDBEntities db = new HostBoostersDBEntities();

        var leagues = db.Leagues.Select(x => new { Value = x.Id, Text = x.League1 }).ToList();
        model.LeagueList = new SelectList(leagues, "Id", "League1");

        if (model.SelectedLeague.HasValue)
        {
            IEnumerable<LeagueDivision> leaguedivisions = db.LeagueDivisions.Where(l => l.LeagueId == model.SelectedLeague.Value);
            model.LeagueDivisionList = new SelectList(leaguedivisions, "Id", "Name");
        }
        else
        {
            model.LeagueDivisionList = new SelectList(Enumerable.Empty<SelectListItem>());
        }
    }
}

查看

@model HostBooster.Models.ViewModelForHostBooster
 @using (Html.BeginForm())
    {
        <div>
            @Html.LabelFor(m => m.Price)
            @Html.TextBoxFor(m => m.Price)
            @Html.ValidationMessageFor(m => m.Price)
        </div>

        <div>
            @Html.LabelFor(m => m.SelectedLeague)
  

此处发生异常 @ Html.DropDownListFor(m =&gt; m.SelectedLeague,Model.LeagueList)

            @Html.DropDownListFor(m => m.SelectedLeague, Model.LeagueList)
            @Html.ValidationMessageFor(m => m.SelectedLeague)
        </div>
        <div>
            @Html.LabelFor(m => m.SelectedLeagueDivision)
            @Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList)
            @Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
        </div>
        <input type="submit" value="save" />
    }

1 个答案:

答案 0 :(得分:1)

在视图中Model.LeagueList为空。它不应该为空。 如果像这样编辑视图代码(例如),那就好了:

<body>
@using (Html.BeginForm())
{
    List<SelectListItem> listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem
    {
        Text = "Example1",
        Value = "Example1"
    });
    listItems.Add(new SelectListItem
    {
        Text = "Example2",
        Value = "Example2",
        Selected = true
    });
    listItems.Add(new SelectListItem
    {
        Text = "Example3",
        Value = "Example3"
    });


    <div>
        @Html.LabelFor(m => m.Price)
        @Html.TextBoxFor(m => m.Price)
        @Html.ValidationMessageFor(m => m.Price)
    </div>

    <div>
        @Html.LabelFor(m => m.SelectedLeague)
        @Html.DropDownListFor(m => m.SelectedLeague, listItems)
        @Html.ValidationMessageFor(m => m.SelectedLeague)
    </div>
    <div>
        @Html.LabelFor(m => m.SelectedLeagueDivision)
        @Html.DropDownListFor(m => m.SelectedLeagueDivision, Model.LeagueDivisionList)
        @Html.ValidationMessageFor(m => m.SelectedLeagueDivision)
    </div>
    <input type="submit" value="save" />
}