Continue giving comment error null in this line:
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Titulo", comentario.PostId);
Probably why you did not enter ModelState.IsValid
.
I'm doing a project there from college. My project is a Blog, something simple. What I need now is in the View of each Article putting a Form below for the person to post the comments of that Article. How do I do? Because the submit would be for ComentarioController
only that it is in the view of the article.
Form that is in View Post:
@model WebBlog.Models.Post
@{
ViewBag.Title = "Post";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<link href="~/Content/Home.css" rel="stylesheet" />
<div class="jumbotron">
<div class="post-title">@Html.DisplayFor(model => model.Titulo)</div>
<div class="postmeta">Postado em 13/05/2014 | por @Html.DisplayFor(model => model.Autor.Nome)</div>
<div class="entry">@Html.DisplayFor(model => model.Conteudo)</div>
</div>
<div class="jumbotron">
<div class="post-title">Comentarios</div>
@foreach (var item in Model.Comentarios)
{
<div class="postmeta">
<div class="comment-author">
<span class="fn">@Html.DisplayFor(modelitem => item.Autor)</span>
<div class="commenta-meta">@Html.DisplayFor(modelitem => item.dataComentario)</div>
</div>
</div>
<div class="entry">@Html.DisplayFor(modelitem => item.comenatrio)</div>
}
</div>
<div class="jumbotron">
<div class="post-title">Envie seu Comentario</div>
@using (Html.BeginForm("Create", "Comentario", FormMethod.Post))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true)
@Html.HiddenFor(model => model.PostId)
<div class="form-group">
<label class="control-label col-md-2" for="dataComentario">Data Comentario:</label>
<div class="col-md-10">
<input type="text" name="dataComentario" class="disabled" disabled="disabled" id="dataComentario" value="@System.DateTime.Now.ToShortDateString()" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Autor">Autor:</label>
<div class="col-md-10">
<input type="text" name="Autor" id="Autor" />
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Comentario">Comentario:</label>
<div class="col-md-10">
<textarea class="control-label col-md-2" id="Comentario" name="Comentario"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Comentar" class="btn btn-default" />
</div>
</div>
</div>
}
</div>
Now the Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include="ComentarioId,PostId,dataComentario,Autor,comenatrio")]
Comentario comentario)
{
if (ModelState.IsValid)
{
db.Comentarios.Add(comentario);
db.SaveChanges();
return RedirectToAction("Post", "Home", new {PostId = comentario.PostId});
}
ViewBag.PostId = new SelectList(db.Posts, "PostId", "Titulo", comentario.PostId);
return View(comentario);
}
The error that occurs is that it does not enter this if (ModelStatte.IsValid) { ... }
.
Then when it goes to the line of ViewBag
it gives the error. I have noticed that Model Comentario
is not being instantiated.