How to send the PartialView input to a controller?

0

I tried to find something that would get me this doubt, but there is nothing concrete for what I want.

I'm using Visual Studio 2015 and MVC 5 and Razor in my project.

I have a form (Create) with a submit in a partial view and I have this partial view rendered in another view (it's a box for comments in a post).

I've also implemented the [HttpPost] of Create () but when I click on submit to submit the comment it does not do the Post for my HttpPost] , how do I send the data to this method? I have the input parameter like this: Create (FormCollection collection).

I know this question may sound trivial, but I'm still on a very initial face.

Partial view code:

@model shanuMVCUserRoles.CommentSet

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Adicione um comentário</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.MemberID)
    @Html.HiddenFor(model => model.PostID)

    <div class="form-group">
        @Html.LabelFor(model => model.Content, htmlAttributes: new {    @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.Content, new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.Content, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Criar" class="btn btn-default" />
        </div>
    </div>
</div>
}

E controller:

    // GET: Comment/Create
    public ActionResult Create()
    {

        CommentSet newComment = new CommentSet();

        return PartialView(newComment);
    }

    // POST: Comment/Create
    [HttpPost]
    public ActionResult Create(FormCollection collection)
    {
        CommentSet newComment = new CommentSet();

        BlogEntities db = new BlogEntities();

        int id = Convert.ToInt32(collection["MemberID"]);

        newComment.Content = collection["Content"];
        newComment.MemberID = Convert.ToInt32(collection["MemberID"]);
        newComment.MemberSet = db.MemberSet.Single(m => m.ID == id);
        newComment.PostID = Convert.ToInt32(collection["PostID"]);
        newComment.VotesDown = 0;
        newComment.VotesUp = 0;



        try
        {
            db.CommentSet.Add(newComment);
            db.SaveChanges();

            return RedirectToAction("Index"); /*AINDA NAO CONFIGUREI ISTO*/
        }
        catch
        {
            return View();
        }
    }

Code where I call the partialview:

<div class="col-xs-6">
            <h5>@item.Content</h5>
            <div class="list-group">
                @foreach (var comment in item.CommentSet)
                {
                    <a href="#" class="list-group-item">
                        <h4 class="list-group-item-heading">@comment.MemberSet.UserName</h4>
                        <p class="list-group-item-text">@comment.Content</p>
                    </a>
                }

                @Html.Partial("_AdicionarComentario", new CommentSet { PostID = item.ID })

            </div>

        </div>
    
asked by anonymous 15.12.2016 / 11:35

1 answer

1

The solution to your problem is to specify in your Form a action, being @using (Html.BeginForm("TuaAction", "TeuController"))

But because you use Typed View, you can change your Create(FormCollection collection) to Create(CommentSet commentSet) .

Staying as follows

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CommentSet commentSet)
        {
            if (ModelState.IsValid)
            {
                db.CommentSet.Add(commentSet);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(commentSet);
        }

But your main problem is that Create is missing the [ValidateAntiForgeryToken] attribute, and this attribute is intended to protect access to your application for counterfeit Http requests, it ensures that the request comes only from your View using a kind of "key".

    
15.12.2016 / 12:04