填充视图模型通过向导方式 [英] Populate ViewModel by a wizard approach

查看:84
本文介绍了填充视图模型通过向导方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现这个伟大的答案,我该怎么办在ASP MVC的向导。结果
<一href=\"http://stackoverflow.com/questions/6402628/multi-step-registration-process-issues-in-asp-net-mvc-splitted-viewmodels-sing/6403485#6403485\">multi-step在asp.net mvc的注册过程中的问题(分裂的ViewModels,单一的模式)

I found this great answer to how I can do a wizard in ASP MVC.
multi-step registration process issues in asp.net mvc (splitted viewmodels, single model)

我只有一个涉及到这个问题。什么是数据填充到视图模型的最佳做法是什么?

I have just one question related to this. What would be the best practice to populate data into the view models?

让我们说,在步骤2我需要显示的数据的列表给用户。该列表数据来自DB。然后,我会继续前进,创造一个构造函数视图模型,或者我应该填充在控制器?

Lets say in step 2 I have the need to display a list of data to the user. The list data comes from the DB. Would I then go ahead and create a constructor to the view model, or should I populate it in the controller?

这是我的code的外观现在。

This is how my code looks right now.

型号

[Serializable]
public class Step1ViewModel : IStepViewModel
{
    public bool MyProperty { get; set; }
}

[Serializable]
public class Step2ViewModel : IStepViewModel
{
    // This needs to be populated with data, I need to display it in a list
    public List<string> MyList { get; set; }
}

[Serializable]
public class Step3ViewModel : IStepViewModel
{
    public bool MyProperty { get; set; }
}

[Serializable]
public class PublishViewModel
{
    public int CurrentStepIndex { get; set; }
    public IList<IStepViewModel> Steps { get; set; }

    public void Initialize()
    {
        Steps = typeof(IStepViewModel)
            .Assembly
            .GetTypes()
            .Where(t => !t.IsAbstract && typeof(IStepViewModel).IsAssignableFrom(t))
            .Select(t => (IStepViewModel)Activator.CreateInstance(t))
            .ToList();
}

public class PublishViewModelBinder : DefaultModelBinder
{
    protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType)
    {
        var stepTypeValue = bindingContext.ValueProvider.GetValue("StepType");
        var stepType = Type.GetType((string)stepTypeValue.ConvertTo(typeof(string)), true);
        var step = Activator.CreateInstance(stepType);
        bindingContext.ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => step, stepType);
        return step;
    }
}

public interface IStepViewModel
{
}

控制器

public ActionResult Publish(int? id)
{
    var publish = new PublishViewModel();
    publish.Initialize();
    return View(publish);
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Publish([Deserialize] PublishViewModel publish, IStepViewModel step)
{
    publish.Steps[publish.CurrentStepIndex] = step;

    if (ModelState.IsValid)
    {

        if (!string.IsNullOrEmpty(Request["next"]))
        {
            publish.CurrentStepIndex++;
        }
        else if (!string.IsNullOrEmpty(Request["prev"]))
        {
            publish.CurrentStepIndex--;
        }
        else
        {
            // TODO: we have finished: all the step partial
            // view models have passed validation => map them
            // back to the domain model and do some processing with
            // the results

            return Content("thanks for filling this form", "text/plain");
        }
    }
    else if (!string.IsNullOrEmpty(Request["prev"]))
    {
        // Even if validation failed we allow the user to
        // navigate to previous steps
        publish.CurrentStepIndex--;
    }

    return View(publish);
}

所以我的问题是,我哪里会填充我的第二步名单?
我首先想到的是将有在第二步视图模型的构造函数。第二个想法是将有在控制器一些逻辑找出哪些步骤IM,并从那里来填充它。但是,这一切听起来有点糟糕。

So my question is, where would I populate my list for Step2? My first thought would be to have a constructor in the Step2 view model. Second thought would be to have some logic in the controller find out which step im at, and populate it from there. But it all sounds a bit bad.

推荐答案

从控制器填充。总是。你不应该用一个视图模型内的上下文,或者更糟的是,实体交互。如果你想抽象数据库的工作,将其移动到存储库或服务,然后只需要您的控制器调用的方法上。

Populate from the controller. Always. You should never be interacting with your context inside of a view model or, worse, an entity. If you want to abstract the database work, move it to a repository or service and then just have your controller call a method on that.

这篇关于填充视图模型通过向导方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆