0

我正在尝试使用来自进一步程序集的新 Html 帮助程序扩展 Serialize() ..

如果你看看:

看法

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<List<MvcApplication2.Models.ProductViewBinding>>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="Microsoft.Web.Mvc" %>>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Index</title>
</head>
<body>
    <div>
    <% using (Html.BeginForm("Index", "Products", FormMethod.Post))
       { %>
       <% int codeIndex = 0;
           foreach (var item in Model)
          { %>
          <%: Html.TextBox("Encryption[" + codeIndex + "].Value") %>
          <%: Html.Serialize("Encryption[" + codeIndex + "].ID", item.ID) %>
          <% codeIndex++; 
        } %>
       <%: Html.SubmitButton("Click meh!") %>
    <% } %>
    </div>
</body>
</html>

模型

[Serializable] 
public class ProductViewBinding
{

    public object ID { get; set; }

    [NonSerialized]
    public object _value;
    public object Value 
    { 
        get { return this._value; } 
        set { this._value = value; } 
    }

}

控制器

[HttpPost]
public ActionResult Index([Deserialize] List<ProductViewBinding> Encryption)
{
    return View("Index", Encryption);
}

发布时它返回 null ......但是如果我删除 [Deserialize] 属性,它会按原样返回,但 ID 仍然加密......关于我可能做错的任何建议?

4

1 回答 1

4

我认为您错过了Serialize助手应该如何工作的重点。您将整个对象图传递给它,该对象图被序列化并存储在一个隐藏字段中,您可以使用该[Deserialize]属性在控制器操作中返回该隐藏字段。您不能将对象的一半序列化而另一半不序列化。


更新:

看到您的评论后,这里有一个解决方法:

模型:

public class ProductViewBinding
{
    public string ID { get; set; }
    public string Value { get; set; }
}

控制器:

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<ProductViewBinding> model = values.Zip(
            ids, (id, value) => new ProductViewBinding { ID = id, Value = value }
        );
        return View("Index", model);
    }
}

看法:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeNs.Models.ProductViewBinding[]>" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="Microsoft.Web.Mvc" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Index</title>
</head>
<body>
    <% using (Html.BeginForm()) { %>
        <%: Html.Serialize("ids", Model.Select(x => x.ID).ToArray(), SerializationMode.Encrypted) %>
        <%for (int i = 0; i < Model.Length; i++) { %>
            <%: Html.TextBox("Values[" + i + "]", Model[i].Value) %>
        <% } %>
        <%: Html.SubmitButton("Click meh!") %>
    <% } %>
</body>
</html>

如果要实现加密,请注意使用的SerializationMode.Encrypted标志,否则用户可以篡改这些值。

于 2010-09-03T06:34:49.350 回答