如何将模型从一个控制器传递到另一个控制器

时间:2020-06-10 12:27:55

标签: c# .net model-view-controller

我是.NET MVC的入门者。我想将模型从一个控制器传递到另一个控制器,该模型包含密码。我的第一个控制器是auth方法,因为我使用了Claims,但是另一个控制器是表决方法,我需要在其中发布ID,密码和投票。我需要在投票控制器中用于数据库中投票确认的密码。 我的代码: 我的模特:

public class LoginModel
{
    public string IDNP { get; set; }
    public string VnPassword { get; set; 
}

我的第一个控制器:

public class AuthController : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(LoginModel model)
    {

        var data = new LoginData();
        data.IDNP = model.IDNP;
        data.VnPassword = model.VnPassword;

        var response = await session.Login(data);
        if (response.Status == true)
        {
            var authclaims = new List<Claim>()
            {
                new Claim(ClaimTypes.Name, data.IDNP),
            };

            var authIdentity = new ClaimsIdentity(authclaims, "User Identity");

            var userPrincipal = new ClaimsPrincipal(new[] {authIdentity});
            HttpContext.SignInAsync(userPrincipal);

            return RedirectToAction("Index", "Vote",new{pass=data.VnPassword});
        }
        else
        {
            return View();
        }
    }
}

我的第二个控制器:

public class VoteController : Controller
{
    private IVote connection;
    public VoteController()
    {
        var bl = new BusinessManager();
        connection = bl.GetVote();
    }
    [Authorize]
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Index(VoteModel vote)
    {
        var identity = (ClaimsIdentity)User.Identity;
        var voteindex = new VoteData();
        voteindex.IDNP = identity.Name;
        voteindex.VnPassword = ;
        voteindex.Party = vote.Party;

        var response = await connection.Vote(voteindex);
        if (response.Status == true)
            return RedirectToAction("Index", "Home");
        else
        {

            return RedirectToAction("Index", "Auth");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

实际上,将Id和密码发送到另一个控制器是个坏主意。它与SRP规则冲突。 您应该只有一个可以获取和使用密码的控制器。

但是,如果要使用此操作,可以使用this

相关问题