使用多重选择列表框时,空引用异常

时间:2018-11-12 18:01:54

标签: asp.net asp.net-mvc-4 razor

我在网页中使用多选列表框将值存储到数据库中,但是我收到了空引用异常,我已经验证了SO中的所有相关问题,但我无法获得确切的解决方案,所以请指导我出问题

型号:

public string[] SelectedTags { get; set; }
public IEnumerable<SelectListItem> AllTags { get; set; }

控制器:

public ActionResult AddPost()
    {
        PostModel Post = new PostModel();           
        Post.SelectedTags = new string[] { "1" };          
        Post.AllTags = GetAllTags();
        return View(Post);
    }
    [HttpPost]        
    public ActionResult AddPost(PostModel Post)
    {
     try
        {
            if (ModelState.IsValid)
            {
                PostRepository PostRepo = new PostRepository();
            }
            return View();
        }
        catch
        {
            return View();
        }
    }
    private IEnumerable<SelectListItem> GetAllTags()
    {
        List<SelectListItem> Tags = new List<SelectListItem>();
        //Add a few cars to make a list of cars
        Tags.Add(new SelectListItem { Value = "1", Text = "mm" });
        Tags.Add(new SelectListItem { Value = "2", Text = "MClaims" });
        Tags.Add(new SelectListItem { Value = "3", Text = "Ejections" });          
        return Tags.AsEnumerable();
    }

查看:

<div class="col-md-12">
 @Html.LabelFor(model => model.SelectedTags, htmlAttributes: new { @class = "control-label col-md-2" })
 @Html.ListBoxFor(model => model.SelectedTags, Model.AllTags, new { @class = "form-control chosen", multiple = "multiple" } )                      
  </div>

脚本:

<script type="text/javascript">
$(".chosen").chosen({ max_selected_options: 5 });
$(".chosen-deselect").chosen({ allow_single_deselect: true });
$(".chosen").chosen().change();
$(".chosen").trigger('liszt:updated');
</script>

执行视图页面时出错

0 个答案:

没有答案