Question

I am creating a simple blog applicaton. I have to add comments to a blog post. I have a line in my code which looks like this:

<input type="hidden" name="Comment.BlogID" value="@Model.Blog.BlogID" />

Basically, I am setting the Comment.BlogID to Model.Blog.BlogID

is there a way to do this using HTML.Hiddenfor.

I tried the code below but that does not work.

@Html.HiddenFor(model => model.Comment.BlogID, new { value = @Model.Blog.BlogID });
Was it helpful?

Solution

You have to set the value in the model of that view to the actual BlogId, if possible in the controller. If that is not possible you can't change the value which is used in the Html.HiddenFor helper later on. You could try

@{ Model.Comment.BlogId = Model.Blog.BlogId; }

OTHER TIPS

@Html.Hidden(model.Comment.BlogID, @Model.Blog.BlogID);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top