【发布时间】:2014-06-23 05:58:58
【问题描述】:
我正在尝试在 asp.net mvc 中制作向导注册表单(多步注册)。
我有一个登录页面(带有初始字段:名称)。当我发布时,它会进入注册页面并序列化名称字段(属于 ViewModel Step1 对象)
一切似乎都运行良好。我正常反序列化对象。
问题出在 ModelState 中。每次都无效,因为隐藏字段被序列化了。
它向我显示了隐藏字段的异常:“从类型 'System.String' 到类型 'Project.ViewModels.Step1' 的参数转换失败,因为没有类型转换器可以在这些类型之间进行转换。”
我做错了什么? 为什么 ModelState 试图验证我的隐藏字段????
我的模型:
public class Wizard
{
public Step1 Step1 { get; set; }
public Step2 Step2 { get; set; }
}
public class Step1
{
[Required]
public string Name{ get; set; }
}
public class Step2
{
[Required]
public string Email { get; set; }
}
我的方法:
[HttpPost]
public ActionResult Register (Wizard user, FormCollection form, [Deserialize] Step1 step1)
{
if (form["Step2Button"] != null)
{
if (ModelState.IsValid) //Is invalid every time
{
user.Step1 = step1;
return View("RegisterFinal", user);
}
return View("Register", user);
}
}
我的观点:
@using (Html.BeginForm("Register", "User", FormMethod.Post))
{
@Html.Serialize("Step1", Model.Step1)
@Html.TextBoxFor(model => model.Step2.Email)
<button name="Step2Button">Continue</button>
}
谢谢
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 serialization