MVC 5 and EF 6 Put another form's view

3

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.

    
asked by anonymous 18.05.2014 / 16:18

1 answer

2

About the form pointing to a Controller and a specific Action :

You can specify where the submit will be pointed to!

<form actiom="~/Comentario/Adicionar" method="post">
    ...
</form>

Or:

@Html.BeginForm("Adicionar", "Comentário", FormMethod.Post)
{
    ...
}

Or even:

<form action="" method="post" id="formComentario">
   ...
</form>

<script type="text/javascript">
    $(function () {
        $("#formComentario").submit(function () {
            $(this).attr("action", "/Comentario/Adicionar");
        });
    });
</script>

About the error in your Controller :

About the error in the Controller, let's get some details

  • You said that the Model Comentario is not being instantiated.

If you are not actually instantiating the value you should see in debugar is null . If this is indeed the case, try typing in your View typing if you do not have:

// algo como
@model Models.Comentario
  • If it is not null , but an instantiated class and empty default values: 0 to int empty for string :

So it can be something I believe in. Which is:
Your view is manually declared and I'm seeing in the Ids and fields as dataComentario and% with%. But I do not think you should have left the fields of your class in the lowerCamelCase pattern. I think you're like:

public DateTime DataComentario { get; set; }
public string Comentario { get; set; }

So check these items and get back what you can find. About not being in the scope of comentario is exactly because your Model has not been instantiated or does not have all the fields in it with what is needed to validate it. As defined in your class annotations, or by your Fluent mapping.

The ASP.NET Framework MVC Framework default is that the fields have exactly the same name as the class property for it to parse for you.

The idea of using Helpers is for Razor to treat this for you, like

@Html.EditorFor(x => x.Comentario)

It will be generated:

<input type="text" id="Comentario" name="Comentario" />
    
18.05.2014 / 17:06