我的视图模型具有内部ViewModel地址属性。
public class CommonViewModel{
public AddressViewModel PreviouslyAddress { get; set; }
}
和地址:
[Serializable]
public class AddressViewModel
{
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string Prefix { get; set; }
public string ZipCode { get; set; }
}
我想序列化它,并在POST上进行deseriliazed。
我用:
@Html.Serialize("PreviouslyAddress", Model.PreviouslyAddress)
但在所有示例中,它们都使用属性DeserializeAttribute
,它只能在方法参数中实现。
EDITED
如何在不创建自定义补丁的情况下执行此操作,并将模型的一部分作为参数发送。
[HttpPost]
public ActionResult Register([DeserializeAttribute] AddressViewModel user, FormCollection userForm)
{
TryUpdateModel(user, userForm.ToValueProvider());
例如,如果可能,我想要这样的东西:
[HttpPost]
public ActionResult Register(CommonViewModel model)
{
TryUpdateModel(user, userForm.ToValueProvider());
public class CommonViewModel{
[DeserializeAttribute]
public AddressViewModel PreviouslyAddress { get; set; }
}
答案 0 :(得分:3)
您可以使用MvcSerializer
类将一些字符串反序列化回模型:
var serializer = new MvcSerializer();
var value = Request["previouslyAddress"]; // this is the contents of the hidden field
var address = (AddressViewModel)serializer.Deserialize(value, SerializationMode.Signed);