简体   繁体   English

如何将 Model 从 controller 传递到另一个 controller

[英]how to pass a Model from a controller to another controller

I'm starter in .NET MVC.我是 .NET MVC 的初学者。 I want to pass a model from one controller to another controller, model contain a password.我想将 model 从一个 controller 传递给另一个 controller,Z20F35E630DAF44DBFA4ZC3F6D 包含密码。 My first controller is auth method, in that I used Claims, but another controller is a vote method there I need to Post an ID, Password and Vote.我的第一个 controller 是身份验证方法,因为我使用了声明,但另一个 controller 是投票方法,我需要发布 ID、密码和投票。 Password I need in vote controller for voting confirmation in database.我在投票中需要的密码 controller 用于在数据库中进行投票确认。 My code: My model:我的代码:我的 model:

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

My first controller:我的第一个 controller:

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();
        }
    }
}

My second controller:我的第二个 controller:

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");
        }
    }
}

Actually, it's a bad idea to send Id and Password to another controller.实际上,将 ID 和密码发送到另一个 controller 是个坏主意。 It conflicts SRP rule.它与 SRP 规则冲突。 You should have only one controller that gets and uses password.您应该只有一个获取和使用密码的 controller。

However, if you want to use this action, you can use this但是,如果你想使用这个动作,你可以使用这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM