samedi 17 janvier 2015

The value 'some value' is invalid when submitting form with checkboxes in MVC 5

I have a form in an MVC 5 application that has several checkboxes to allow different categories to be selected. However when submitting, I get the message The value 'some value' is invalid. That appears where the error message is for the checkboxes. I have looked and not found a solution.


I have an editor template:



@Modeltype ienumerable(Of be_Categories)
@For Each x In Model
@<label>@x.CategoryName</label>
@<input type="checkbox" name="PostCategory" value="@x.CategoryID"/>
Next


And in the form I call:



@Html.LabelFor(Function(model) model.PostCategory, htmlAttributes:=
New With {.class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(Function(model) model.PostCategory,
New With {.htmlAttributes = New With {.class = "form-control"}})
@Html.ValidationMessageFor(Function(model) model.PostCategory, "",
New With {.class = "text-danger"})
</div>


And in controller:



Public Function CreateNewPost(ByVal post As be_PostsViewModel) As ActionResult
If ModelState.IsValid Then

Dim p As New be_PostsViewModel
p.PostTitle = post.PostTitle
p.PostDateCreated = post.PostDateCreated
p.IsPublished = post.IsPublished
p.PostGuid = Guid.NewGuid
p.PostSummary = post.PostSummary
p.PostText = post.PostText
p.PostCategory = post.PostCategory
_POSTREPO.Insert(p)

Return RedirectToAction("Index")
End If
Return View(post)
End Function


Can someone tell me where I am going wrong?





Aucun commentaire:

Enregistrer un commentaire