gpt4 book ai didi

.net - 由于未在下拉框列表等中发送所有数据而导致模型状态无效时必须重新填充 View 模型

转载 作者:行者123 更新时间:2023-12-01 02:22:20 25 4
gpt4 key购买 nike

在我的项目中,我创建了一个调查,我将此调查链接到一家公司并选择用户参与。我选择调查模板,添加额外的问题并设置有关调查的信息,例如开始和结束日期。

总而言之,结果是一个复杂的 View ,其中包含来自域的许多部分的数据。
我为此 View 创建了一个 ViewModel,其中包括所有公司的列表等内容(因为我需要在下拉列表中选择公司)。

现在令人烦恼的是,当我提交保存时,如果我有模型状态错误,那么我想重新显示 View ,但是由于我获取的所有数据(例如所有公司的列表)都没有从客户端发送(即使 View 模型支持它)在再次显示表单之前,我必须再次填充所有这些列表/变量。

我想我可以创建一个创建下拉列表的 View ,但是由于下拉列表需要将它的值插入到我在 CompanyId 下的调查中,这也不明显。

我认为自定义模型绑定(bind)器也可以解决问题。我在这里要求对那里的最佳解决方案有所了解。如果我宁愿重新考虑整个战略。

最佳答案

模型绑定(bind)器可能有助于该过程,但我不认为它是一个完整的解决方案。一方面,模型绑定(bind)器在实例化模型时可能不“知道”您的意图。例如,它如何知道您稍后会在操作方法中认为模型无效?

我通常有一个单独的方法(甚至是一个单独的类),其唯一目的是管理适合情况的 View 模型数据的构建。

然后 action 方法告诉这个助手它想要什么,允许它作为进程的管理者来服务于它的目的。例如, Controller 可以决定:

  • 需要准备一个新的 View 模型。
  • 需要准备一个新的 View 模型,但使用查询字符串中的值初始化选择属性。
  • 应该构建 View 模型来表示现有的域模型。
  • 应保留 View 模型的用户写入数据,但应重建其他字段(例如您的下拉列表示例)。

  • 我将这种 View 模型的构建称为“组合”。本质上,您正在将向 View 呈现完整 View 模型所需的数据汇总在一起。这些数据可能来自不同的地方。

    我描述了 composition process in more detail here .我已经编写了一个完整的框架来支持 ASP.Net MVC 的组合模式(封闭源代码,只是由于时间的原因)。我发现它使支持复杂的 View 模型变得更加容易,并且大大提高了我的代码重用性。

    简单示例

    此示例将进程保留在 Controller 内部(而不是单独的类),并专注于指定一些简单的选项。
    [Flags]
    public enum CompositionOptions
    {
    PopulateFromDomainModel = 1,
    Hydrate = 2
    }

    [HttpGet]
    public ActionResult Edit( int id)
    {
    var model = new ViewModel();
    // the controller states exactly what it wants
    Compose( model, CompositionOptions.PopulateFromDomainModel | CompositionOptions.Hydrate, id );
    }

    [HttpPost]
    public ActionResult Edit( ViewModel model )
    {
    if( !ModelState.IsValid )
    {
    // Rebuild values which weren't contained in the POST. Again, the
    // controller states exactly what it needs.
    Compose( model, CompositionOptions.Hydrate );
    return View( model );
    }

    // Use POST-redirect-GET pattern, allowing the page to reload with the changes
    return RedirectToAction( "Edit", new { id = model.Id } );
    }

    private void Compose( ViewModel model, CompositionOptions options, int? id = null )
    {
    // This logic can become quite complex, but you are generally checking for
    // existing data source (domain models) and what you should populate and
    // what fields you should preserve.

    if( id != null && options.HasFlag( CompositionOptions.PopulateFromDomainModel ) )
    {
    // get your domain model from a repository and populate the
    // properties of the view model
    }

    if( options.HasFlag( CompositionOptions.Hydrate ) )
    {
    // set values on the view model which won't be included in
    // a POST, and thus must be rebuilt with every roundtrip
    }
    }

    关于.net - 由于未在下拉框列表等中发送所有数据而导致模型状态无效时必须重新填充 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336787/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com