【发布时间】:2020-06-10 12:27:55
【问题描述】:
我是 .NET MVC 的初学者。我想将模型从一个控制器传递到另一个控制器,模型包含密码。我的第一个控制器是身份验证方法,因为我使用了声明,但另一个控制器是投票方法,我需要发布 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");
}
}
}
【问题讨论】:
-
为什么要用投票存储密码?您应该在登录后调用业务层方法来存储来自 Auth Controller 的投票。
-
为了清楚起见,您正在尝试使用
HttpPost版本的Index? -
另外,你的
VoteModel类是什么样的? -
@PatrickMcvay 我删除了 IActionResult 然后我发布了。 VoteModel 包含公共字符串 Party { get;放; }。这是一个用户选择的聚会。
-
我觉得这是一个笑话,你如何要求控制器将数据发送到你使用的另一个控制器接口,例如观看亚瑟王电影,你会得到这个
标签: c# .net model-view-controller