我在我的一个视图中使用帮助器Html.Serialize
来序列化一个对象,然后在我的Controller中使用MvcSerializer再次反序列化它,如下所示:
(RegistrationViewModel) new MvcSerializer().Deserialize(serializedObject);
之前这个工作正常,但在升级到MVC3(和MVC3 Futures)后,Deserialize
方法已经改变,现在它还有一个参数:SerializationMode。
我可以设置为Signed
或EncryptedAndSigned
。
无论我使用哪一个,我都会得到一个SerializationException
,要求我验证数据是否使用我尝试反序列化的相同模式进行序列化。
在我看来,由于某种原因,IntelliSense不适用于Serialize方法,所以我不知道是否还有任何额外的重载。
但问题很简单:我现在如何在MVC3
中反序列化我在控制器中使用Html.Serialize
在序列中序列化的数据?
编辑 - 添加了某些代码
来自控制器
私人RegistrationViewModel registeredAccount;
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var serialized = Request.Form["regData"];
if (serialized != null)
{
registeredAccount = (RegistrationViewModel) new MvcSerializer().Deserialize(serialized, SerializationMode.EncryptedAndSigned);
TryUpdateModel(registeredAccount);
}
else
registeredAccount = (RegistrationViewModel)TempData["regData"] ?? new RegistrationViewModel();
}
public ActionResult Index()
{
if (Request.Form["nextButton.x"] != null)
return RedirectToAction("Details");
return View(registeredAccount);
}
从视图(序列化调用位于表单的开头)
@model VektNed.Models.RegistrationViewModel
...
<div class="BodyContainer RegistrationBody">
@using (Html.BeginForm()){
@Html.Serialize("regData", Model, SerializationMode.EncryptedAndSigned)
<div class="RegistrationGroup">
<p class="Description">e-posten din benyttes som brukernavn for å logge inn.</p>
<p><label for="Account_Email">e-post</label> @Html.EditorFor(x => x.Account.Email)<span class="ErrorMessage"></span></p>
<p><label for="RepeatEmail">gjenta e-post</label> <input type="text" name="RepeatEmail" id="RepeatEmail" /><span class="ErrorMessage"></span></p>
<p><label class="CheckboxLabel" for="ShareEmail">del din e-post med andre brukere. </label> @Html.EditorFor(x => x.Account.ShareEmail)</p>
</div>
<div class="RegistrationGroup">
<p><label for="Account_FirstName">fornavn</label> @Html.EditorFor(x => x.Account.FirstName)<span class="ErrorMessage"></span></p>
<p><label for="Account_LastName">etternavn</label> @Html.EditorFor(x => x.Account.LastName)<span class="ErrorMessage"></span></p>
</div>
<div class="RegistrationGroup">
<p><label for="Password">ønsket passord</label> @Html.EditorFor(x => x.Password)<span class="ErrorMessage"></span></p>
<p><label for="ConfirmPassword">gjenta passord</label> <input type="password" name="ConfirmPassword" id="ConfirmPassword" /><span class="ErrorMessage"></span></p>
</div>
<div class="RegistrationGroup">
<input class="NextAlone" name="nextButton" type="image" src="../../Images/button_next.png" alt="Neste" />
</div>
}
</div>
...
答案 0 :(得分:2)
为什么要手动反序列化这样的对象,而不是使用[Deserialize]
atrtribute:
[HttpPost]
public ActionResult Index([Deserialize] RegistrationViewModel registrationModel)
{
...
}
就第二个参数而言,它必须与您在视图中序列化时使用的枚举值相匹配。例如,如果你在视图中做了:
@Html.Serialize("registrationModel", SomeModel, SerializationMode.EncryptedAndSigned)
你会使用:
[HttpPost]
public ActionResult Index([Deserialize(SerializationMode.EncryptedAndSigned)] RegistrationViewModel registrationModel)
{
...
}
或者如果您更喜欢体力劳动:
var registrationModel = (RegistrationViewModel) new MvcSerializer()
.Deserialize(serializedObject, SerializationMode.EncryptedAndSigned);