Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think you are missing the point of how the <code>Serialize</code> helper is supposed to work. You pass it an entire object graph which is serialized and stored in a hidden field that you could get back in a controller action using the <code>[Deserialize]</code> attribute. You cannot have half of your object serialized and the other half not.</p> <hr> <p>UPDATE:</p> <p>After seeing your comment here's a workaround:</p> <p>Model:</p> <pre><code>public class ProductViewBinding { public string ID { get; set; } public string Value { get; set; } } </code></pre> <p>Controller:</p> <pre><code>public class HomeController : Controller { public ActionResult Index() { var model = new[] { new ProductViewBinding { ID = "1", Value = "value 1" }, new ProductViewBinding { ID = "2", Value = "value 2" }, }; return View(model); } [HttpPost] public ActionResult Index( [Deserialize(SerializationMode.Encrypted)]string[] ids, string[] values ) { IEnumerable&lt;ProductViewBinding&gt; model = values.Zip( ids, (id, value) =&gt; new ProductViewBinding { ID = id, Value = value } ); return View("Index", model); } } </code></pre> <p>View:</p> <pre><code>&lt;%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage&lt;SomeNs.Models.ProductViewBinding[]&gt;" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;%@ Import Namespace="Microsoft.Web.Mvc" %&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt; &lt;head id="Head1" runat="server"&gt; &lt;title&gt;Index&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;% using (Html.BeginForm()) { %&gt; &lt;%: Html.Serialize("ids", Model.Select(x =&gt; x.ID).ToArray(), SerializationMode.Encrypted) %&gt; &lt;%for (int i = 0; i &lt; Model.Length; i++) { %&gt; &lt;%: Html.TextBox("Values[" + i + "]", Model[i].Value) %&gt; &lt;% } %&gt; &lt;%: Html.SubmitButton("Click meh!") %&gt; &lt;% } %&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Notice the <code>SerializationMode.Encrypted</code> flag which is used if you want to achieve encryption, otherwise the user can tamper with the values.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload