I've searched everywhere for an answer to this question, perhaps I am unlucky. This is such a simple thing but I'd rather start here just in case it takes me longer than expected.
So I have the simplest view ever.
@model System.String
@using (Html.BeginForm("AdminDeletePost", "Home", FormMethod.Post))
{
@Html.HiddenFor(m => m)
<h3>Are you sure you want to delete the following user?</h3>
<br />
<h4>@Model</h4>
<br />
<h6>To confirm deletion, click <i>Save</i>.</h6>
<h6>To cancel, click <i>Cancel</i>.</h6>
}
All I want to do, is pass the value of Model (which is a string) back to the AdminDeletePost action in the Home controller. So it's laid out just like one of my typical forms. Now that it's simple though, I can't get it to work. I should add, that the <h4>@Model</h4>
above does display the correct value. The value of the model is not null. I'm assuming that the HiddenHelper
method is passing metadata of some sort to the InputHelper
method, one item of which is the Name parameter. However, string isn't something that I can annotate like a normal model. Perhaps I'm looking at this in the wrong way.
I can already guess somebody is going to suggest not going the route that I am due to it's simplicity (i.e. not posting the entire form but rather the individual string). However, due to the layout of the application, and maintaining consistency among all of my forms, this is the ideal setup for now.
Here is the stack...
Has anybody else had any relatively similar issues before?
These helpers can only take properties of a model; not the entire model.
Otherwise, it would have no way to figure out what name the input should have.
You should either switch to a view model object with one string property (recommended) or write an <input type="hidden">
yourself with appropriate name and value.
Either way, the name must match your action's parameter name for request binding to work.