How can i pass hidden field value from view to controller ASP.NET MVC 5

asp.net-mvc c# entity-framework

Question

The following is how I'm attempting to transmit a hidden field's value from the view to the controller:

@Html.HiddenFor(model => model.Articles.ArticleId) 

and tried as well

<input type="hidden" id="ArticleId" name="ArticleId" value="@Model.Articles.ArticleId" />

Help would be much appreciated. In both cases, the value of ArticleId is 0, but when I use TextboxFor, I can see the actual ArticleId.

This is it.

View

@model ArticlesCommentsViewModel
....
@using (Html.BeginForm("Create", "Comments", FormMethod.Post))
{
<div class="row">
    <div class="col-xs-10 col-md-10 col-sm-10">
        <div class="form-group">
            @Html.LabelFor(model => model.Comments.Comment, new { @class = "control-label" })
            @Html.TextAreaFor(m => m.Comments.Comment, new { @class = "ckeditor" })
            @Html.ValidationMessageFor(m => m.Comments.Comment, null, new { @class = "text-danger"})
        </div>
    </div>
</div>

<div class="row">

        @*@Html.HiddenFor(model => model.Articles.ArticleId)*@
    <input type="hidden" id="ArticleId" name="ArticleId" value="@Model.Articles.ArticleId" />
</div>

<div class="row">
    <div class="col-xs-4 col-md-4 col-sm-4">
        <div class="form-group">
            <input type="submit" value="Post Comment" class="btn btn-primary" />
        </div>
    </div>
</div>
}

Controller

    // POST: Comments/Create
    [HttpPost]
    public ActionResult Create(CommentsViewModel comments)//, int ArticleId)
    {
        var comment = new Comments
        {
            Comment = Server.HtmlEncode(comments.Comment),
            ArticleId = comments.ArticleId,
            CommentByUserId = User.Identity.GetUserId()
        };
    }

Model

public class CommentsViewModel
{
    [Required(ErrorMessage = "Comment is required")]
    [DataType(DataType.MultilineText)]
    [Display(Name = "Comment")]
    [AllowHtml]
    public string Comment { get; set; }

    public int ArticleId { get; set; }
}

ViewModel

public class ArticlesCommentsViewModel
{
    public Articles Articles { get; set; }
    public CommentsViewModel Comments { get; set; }
}
1
7
9/10/2016 2:08:02 AM

Accepted Answer

In the perspective, the model isArticlesCommentsViewModel The parameter in your POST method must thus match. How you utilise

@Html.HiddenFor(model => model.Articles.ArticleId)

is accurate, however you need switch the approach to

[HttpPost]
public ActionResult Create(ArticlesCommentsViewModel model)

and the model will be correctly bound.

On a related point,ArticlesCommentsViewModel should only include the characteristics you need in the view and should not include data models. When typeofArticles includes properties with characteristics for validation,ModelState due to your failure to publish all of the characteristics ofArticle .

However, becauseCommentsViewModel has a property for alreadyArticleId if so, you could just use

@Html.HiddenFor(model => model.Comments.ArticleId)

the POST technique, too.

[HttpPost]
public ActionResult Create([Bind(Prefix="Comments")]CommentsViewModel model)

How successfully remove the prefix "Comments"

5
9/10/2016 2:10:05 AM

Popular Answer

ZZZ_tmp


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow