将viewmodel从一个方法传递到另一个方法(post)

时间:2013-02-03 19:17:43

标签: asp.net-mvc viewmodel

我有一个向导类型表单,我希望将模型从第一步保留到最后一步。

这就是我现在正在做的事情,但我似乎无法将模型从第3步保留到第4步。

[HttpPost]
public ActionResult Register(MemberViewModel model)
{
    var mobile = model.Mobile;
    var xml = MemberclubHelper.CreateVerificationTokenXML(mobile, "47");
    MemberclubHelper.SendVerificationToken(xml);
    return RedirectToAction("RegisterStep1", new { mobile = mobile });
}

public ActionResult RegisterStep1(string mobile)
{
    var model = new MemberViewModel();
    model.Mobile = mobile;
    return View("Register/Step1", model);
}

[HttpPost]
public ActionResult RegisterStep1(MemberViewModel model)
{
    var interests = (from interest in model.Interests where interest.isChecked select interest.value).ToList();
    var passing = (from pass in model.Passing where pass.isChecked select pass.value).ToList();

    var xml = MemberclubHelper.CreateMemberProfileXML(model.Mobile, model.FirstName, model.FirstName,
        model.Email1, model.Zipcode, model.SelectedKid1, model.SelectedKid2, model.SelectedKid3,
        model.Gender.ToString(), interests, passing);
    model.XmlToSend = xml;
    if (xml.Contains("error"))
    {
        model.ErrorMessage = xml;
        return View("Register/Step1", model);
    }

    return View("Register/Step2", model);
}

[HttpPost]
public ActionResult RegisterStep2(MemberViewModel model)
{
    var result = MemberclubHelper.SendCreateUser(model.XmlToSend, model.Password);
    if (result.Contains("error"))
    {
        model.ErrorMessage = result;
        return View("Register/Step2", model);
    }

    else
    {
        return View("Register/Finished");
    }
}

1 个答案:

答案 0 :(得分:0)

我认为通过为每个步骤创建单独的视图模型(即MemberViewModelStep1)可能会更好。对我来说,这似乎只会在每个步骤中设置一些MemberViewModel属性,除非您通过隐藏输入或其他一些机制在步骤之间传递大量状态。

或者,您是否考虑过使用JavaScript在各个步骤之间构建该状态,然后使用完全填充的MemberViewModel提交单个帖子?