Alright, I’m trying to pass a Dictionary, where key = int, and value = Prototype into my view.
Prototype:
public class Prototype
{
public string Value { get; set; }
public string PropertyName { get; set; }
public Options Type { get; set; }
}
After the Dictionary has been passed into my view I’m doing a foreach loop to render each of the KeyValuePair:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Dictionary<System.Int32,MvcApplication1.Models.Prototype>>" %>
<%@ Import Namespace="SecuredFormExample.Code" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Index</title>
</head>
<body>
<div>
<% Html.BeginForm("Save", "Products", FormMethod.Post); %>
<% foreach (KeyValuePair<int, Prototype> p in Model)
{ %>
<%: Html.Label(p.Value.PropertyName) %>
<%: Html.TextBox("Value") %>
<% } %>
<p><input type="submit" value="submit" /></p>
<% Html.EndForm(); %>
</div>
</body>
</html>
Now is the question, is it possible to pass the other values in the KeyValuePair value part back to the action aswell, or do I need to throw them all into hidden fields?
If you want to get the values back into the action you are posting to you need to send them along the request. Using hidden fields is one way of doing it. Another way is to only send an identifier which will allow you to fetch those values from a repository in the controller action you are posting to, the same way you are doing it in the controller action which renders the form.
If you are worried about users tampering with hidden fields you could serialize and encrypt entire objects using the Html.Serialize.
UPDATE:
Here’s an example to expand on my second proposition which was to use an id:
and in your controller action: