我的控制器:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(BlogCompModel blogmodel)
{
   if (ModelState.IsValid)
   {
      ...
   }
}


我的看法:

@model BlogProject.Models.BlogCompModel

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    ...


   <div class="form-group">
      @Html.LabelFor(model => model.BlogCompModel.posts, "Property", new { @class = "control-label col-md-2" })
      <div class="col-md-10">
        @Html.ListBoxFor(model => model.posts, new MultiSelectList(Model.posts, "post_ID", "postTitle"))
        @Html.ValidationMessageFor(model => model.posts.posts)
      </div>
   </div>

}


发布错误消息:

"The parameter conversion from type 'System.String' to type 'BlogProject.Models.Posts' failed because no type converter can convert between these types."}  System.Exception {System.InvalidOperationException}


如您所见,我不确定如何将HTML Multiselect列表从Post_ID的集合转换为帖子的ICollection。

谢谢!

最佳答案

您可以在BlogCompModel模型中添加另一个属性,它将所有选定的帖子包装在其中。

public class BlogCompModel
{
    //
    public string[] selectedPosts { get; set; }
}


然后在您看来:

@Html.ListBoxFor(model => model.selectedPosts ,
                 new MultiSelectList(Model.posts, "post_ID", "postTitle"))
@Html.ValidationMessageFor(model => model.selectedPosts )

关于c# - ASP MVC EF MultiSelect HTTP POST,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21944328/

10-11 21:45