Skip to content

Instantly share code, notes, and snippets.

@codebeaulieu
Created May 20, 2015 22:35
Show Gist options
  • Save codebeaulieu/f0e63d6bb66c6c660929 to your computer and use it in GitHub Desktop.
Save codebeaulieu/f0e63d6bb66c6c660929 to your computer and use it in GitHub Desktop.
Create.cshtml | Stack Overflow | How to resolve ModelState.IsValid = false while binding a List type?
@model BlogEngine.Models.Post
@using BlogEngine.Models
@{
ViewBag.Title = "Create";
Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<div class="page-content">
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Post</h4>
@Html.HiddenFor(model => model.Id)
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.URL, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@Html.EditorFor(model => model.URL, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.URL, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.IntroText, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@Html.TextAreaFor(model => model.IntroText, new { htmlAttributes = new { @class = "form-control edit-intro" } })
@Html.ValidationMessageFor(model => model.IntroText, "", new { @class = "text-danger" })
</div>
</div>
@*
<div class="row margin-bottom">
<!-- Button trigger modal -->
<div class="col-md-offset-2 col-md-4">
<button type="button" class="btn btn-default btn-sm" data-toggle="modal" data-target="#imgUploadModal">
Upload Image
</button>
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.Body, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@Html.TextAreaFor(model => model.Body, new { htmlAttributes = new { @class = "form-control edit-body" } })
@Html.ValidationMessageFor(model => model.Body, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tags, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@for (var i = 0; i < 4; i++)
{
@Html.HiddenFor(m => m.Tags[i].Id)
@Html.EditorFor(model => model.Tags[i].Name)
}
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Author, htmlAttributes: new { @class = "control-label col-md-2 col-md-offet-3" })
<div class="col-md-7">
@Html.EditorFor(model => model.Author, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Author, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
</div>
<!-- Modal -->
<div class="modal fade bs-example-modal-sm" id="imgModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Select an Image</h4>
</div>
<div class="modal-body select-body">
@Html.Action("GetImages", "Images", new { area = "Admin" })
</div>
<button type="button" class="btn btn-blog-engine select-close" data-dismiss="modal">Select Image</button>
</div>
</div>
@* // ask what the best way to do this, why does the above not break my model but this one does
<div class="modal fade bs-example-modal-lg" id="imgUploadModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-content">
@Html.Action("Create", "Images", new { area = "Admin" })
</div>
</div>
*@
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment