【问题标题】:Html.Serialize error in modelState模型状态中的 Html.Serialize 错误
【发布时间】: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


    【解决方案1】:

    好的,我只是在验证之前在隐藏字段中使用了 ModelState.Remove。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-14
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多