I am following this tutorial. Can anyone please explain to me how I can make the textboxes bind back to the Dictionary? Right now all that happens is the data is being displayed in the textboxes – but if I change the textboxes, how do I bind back to the object?. What am I missing? Below is my code:
<input type="text" name="@Model.ExpirimentToRemove[i].Value.Title" value="@Model.ExpirimentToRemove[i].Value.Title"/>
<input type="text" name="@Model.ExpirimentToRemove[i].Value.PreviewDescription" value="@Model.ExpirimentToRemove[i].Value.PreviewDescription"/>
<input type="text" name="@Model.ExpirimentToRemove[i].Value.FullDescription" value="@Model.ExpirimentToRemove[i].Value.FullDescription"/>
The
nameof your text input field is wrong. You have put the value of the model instead of naming it appropriately as explained in the Hanselman’s blog post.So let’s assume that you have some view model:
and a main view model containing the dictionary:
and your POST controller action takes this view model as parameter:
In order to correctly bind to this view model you could have the following:
In this example the key of the dictionary is a simple string value but you could also use a complex type.