如何将视图模型从 Create 传递回 mvc 中的 Edit 视图

How can I pass the viewmodel from Create back to the Edit view in mvc

[HttpGet]
public ActionResult Edit(int? id) //View
{
       PlayerSettingViewModel view = new PlayerSettingViewModel();
       if (id != null)
       {
              AccountDTO model = _AccountsBLL.GetAccountById(id.Value);
              if (model != null)
              {
                    if (model.AccountId != null)
                    {
                        AccountDTO Account = _AccountsBLL.GetAccount(model.AccountId);

                        view = new PlayerSettingViewModel
                        {
                            Id = Account .Id,
                            AccountType = Account.AccountType
                            Username = Account.Username ,
                            Password = Account.Password ,
                        }

                    }
              }
       }
       return View(view);
}
public ActionResult Create(PlayerSettingViewModel view) //Action
{
      ///Passing Data and creating it
      if(model.id > 0)
      {
            return Redirect("Player","Index")//When successfully created
      }else
      {
            return REdirect("Player", "Edit")//When fail to creat
      }
}

我的问题是,当用户创建信息失败时,如何将用户插入的视图模型数据从创建操作传递到编辑页面。此操作是为了帮助用户不需要重新填写必填字段,而只需重新插入无效的填写。

您不应重定向,因为模型状态会丢失。

如果无法在数据库中保存模型,我们所做的是 return 将模型返回到视图,它将在 UI 上重新填充,例如:

  //Passing Data and creating it
  if(model.id > 0)
  {
        //When successfully created
  }
  else
  {
        return View("Edit",model); //When fail to creat
  }

这将呈现编辑视图,其中发布到创建操作的数据填充回视图。