How to pass int parameter from partialview to controller

I have a commentForm that is my partial view,when it is called i want to send an integer id to controller.

this is my partialview

@model *****.Models.Comments

@using (Html.BeginForm("CommentForm", "Product", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>Comments</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="hide">
    <div class="form-group">
        @Html.LabelFor(model => model.ProductID, "ProductID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("ProductID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.ProductID, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.UserID, "UserID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("UserID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.UserID, "", new { @class = "text-danger" })
        </div>
    </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.CommentText, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CommentText, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CommentText, "", 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>

}

And in a view that is called product and has its model I call partialview

@if (User.Identity.IsAuthenticated)
{   
    @Html.RenderPartial("CommentForm", Model.ProductID)

it errors on Model.ProductID

Error:can not implicity convert type void to obj

and this is my controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult CommentForm(Comments comments, int id)
{
    int userid = database.Users.First(u => u.UserName == User.Identity.Name).UserID;
    if (ModelState.IsValid)
    {
        comments.ProductID = id;
        comments.UserID = userid;
        comments.CommentDate = DateTime.Now;
        comments.IsApproved = false;
        database.Comments.Add(comments);
        database.SaveChanges();
        return View(comments);
        //return View("CommentSubmitSuccess");
    }
    ViewBag.ProductID = new SelectList(database.Products, "ProductID", "ProductTitle");
    ViewBag.UserID = new SelectList(database.Users, "UserID", "UserName");
    return View("ShowProduct");
}

1 answer

  • answered 2018-01-13 17:33 Mansoure

    this is comment model

      public partial class Comments
    {
        public int CommentID { get; set; }
        public int ProductID { get; set; }
        public int UserID { get; set; }
        public string CommentText { get; set; }
        public Nullable<bool> IsApproved { get; set; }
        public System.DateTime CommentDate { get; set; }
    
        public virtual Products Products { get; set; }
        public virtual Users Users { get; set; }
    }