Asp.net mvc MVC控制器到控制器数据

Asp.net mvc MVC控制器到控制器数据,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,MVC中有没有一种方法可以将信息从一个控制器传递到另一个控制器?我的角色模型如下所示: public class Character { [Key] public string CharacterID { get; set; } public string UserID { get; set; } public string Name { get; set; } public int Str { get;

MVC中有没有一种方法可以将信息从一个控制器传递到另一个控制器?我的角色模型如下所示:

 public class Character
    {
        [Key]
        public string CharacterID { get; set; }
        public string UserID { get; set; }
        public string Name { get; set; }
        public int Str { get; set; }
        public int Con { get; set; }
        public int Dex { get; set; }
        public int Int { get; set; }
        public int Wis { get; set; }
        public int Cha { get; set; }
        public int BaseAttack { get; set; }
    }
 public class Weapons
    {
        [Key]
        public string WeaponID { get; set; }
        public string UserID { get; set; }
        public string CharacterID { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string Range { get; set; }
        public int Damage { get; set; }
        public int Crit { get; set; }
        public int CritMultiplier { get; set; }
        public string Hands { get; set; }
        public string Distance { get; set; }
    }
还有一个单独的武器模型,如下所示:

 public class Character
    {
        [Key]
        public string CharacterID { get; set; }
        public string UserID { get; set; }
        public string Name { get; set; }
        public int Str { get; set; }
        public int Con { get; set; }
        public int Dex { get; set; }
        public int Int { get; set; }
        public int Wis { get; set; }
        public int Cha { get; set; }
        public int BaseAttack { get; set; }
    }
 public class Weapons
    {
        [Key]
        public string WeaponID { get; set; }
        public string UserID { get; set; }
        public string CharacterID { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string Range { get; set; }
        public int Damage { get; set; }
        public int Crit { get; set; }
        public int CritMultiplier { get; set; }
        public string Hands { get; set; }
        public string Distance { get; set; }
    }

要创建武器,首先需要创建一个指定ID的角色,我希望能够将该ID传递到我的武器控制器的create方法中。有办法做到这一点吗?谢谢

我倾向于通过路由将角色id传递给创建武器操作,或者作为路径的一部分的路由令牌,或者通过查询字符串。确保检查武器是否可以逻辑上与id对应的角色相关联


您也可以使用
TempData
Session
传递id,但考虑到默认情况下两者都会占用web服务器上的内存,简单的选择是使用路由。此外,除非在访问
TempData
后调用
TempData.Keep(“key”)
,否则在首次访问
TempData
后,该值将从中删除,如果用户刷新浏览器窗口,则可能会导致问题。

您可以为此使用TempData。TempData仅在两个请求之间存储数据。设置TempData时,启动的下一个请求可以从TempData中检索值,并且对于任何后续请求,该值都将被擦除

[HttPost]
public ActionResult CreateCharacter() 
{

  // creates charaeters here and sets the tempdata
  TempData['CharacterId'] = 50; 

  return RedirectToAction('CreateWeapon');
}

[HttpGet]
public ActionResult CreateWeapon() 
{
  var weaponModel = new WeaponModel() { CharacterId = (int)TempData['CharacterId'] };
  return View(weaponModel);
}
在您的视图中,只需为CharacterId设置一个隐藏项,因此,如果您的post未通过验证或需要重新显示视图,它将被持久化

@Html.HiddenFor(e => e.CharacterId);
同样,这只是一种方法,仅当您不想在url中传递CharacterId时

您也可以通过在url中传递来实现这一点:

[HttPost]
public ActionResult CreateCharacter() 
{

  // creates charaeters here and sets the tempdata

  return RedirectToAction('CreateWeapon', new { characterId = 50 });
}

[HttpGet]
public ActionResult CreateWeapon(int characterId) 
{
  var weaponModel = new WeaponModel() { CharacterId = characterId };
  return View(weaponModel);
}

您可以使用
RedirectToAction()
,尽管标题为这将导致浏览器重定向

return RedirectToAction("CreateWeapon", "Weapon", new { id = yourid });


编辑:您的普通对象属性名称和操作方法变量需要匹配才能做到这一点。

不要为此使用TempData。TempData在第一次访问后被擦除。那么,如果用户刷新页面会发生什么呢?TempData中的数据已经消失。当它被删除时,您的模型中已经有了ID。我的操作来自一个视图,因此这行代码位于我的character index view@Html。ActionLink(“创建武器”、“创建”、“武器”)是否可以在那里设置临时数据,或者我必须首先在控制器方法中进行设置?不太可能,因为用户必须在表单保存到模型之前先填写表单。如果在保存前按F5,则id将消失。否则,您将在重定向之前将其保存到模型中。@NerdyWizard您可以使用我在回答中为
RedirectToAction()
提供的相同参数,并将其应用到
@Html.ActionLink()