C# ASP MVC EF MultiSelect HTTP POST

C# ASP MVC EF MultiSelect HTTP POST,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我的控制器: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(BlogCompModel blogmodel) { if (ModelState.IsValid) { ... } } @model BlogProject.Models.BlogCompModel @using (Html.BeginForm()) { @Html.AntiForgeryToken()

我的控制器:

[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}
我的观点:

[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 of Post


谢谢

您可以在您的
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 )

在我的博客MVC应用程序中,我有一个multiselect for标记,这里是如何在Create action中添加这些标记的

Index.cshtml

@model MyBlog.Core.Post

 @Html.ListBox("PostTags", (MultiSelectList)ViewBag.MultiSelectList, new { @class = "form-control" })
控制器

public ActionResult CreatePost([Bind(Include = "Id,Title,ShortDescription,Description,Published,PostedOn,ModifiedOn,CategoryId")] Post post, int[] postTags)
        {
            if (postTags != null)
            {
                foreach (var t in postTags)
                {
                    var tag = _dbTag.GetById(t);
                    post.Tags.Add(tag);
                }
            }
           // save to database and other stuff
           return View(post);
        }

我对MVC不太了解,但这里有一个链接可以帮助您()可以将视图中的返回类型绑定到模型中的类型(ICollection)吗?或者我需要迭代我的控制器的基本类型并填充ICollection吗?是的,这是可能的。我不知道你的
BlogCompModel
模型是什么样子,但是从你提供的代码中,你只需要添加一个字段。如果您想了解有关将列表绑定到模型的更多信息,请查看以下链接: