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 });
有帮助吗?

解决方案

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; }

其他提示

@Html.Hidden(model.Comment.BlogID, @Model.Blog.BlogID);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top