我需要将数据从一个控制器操作传递到另一个控制器操作。 我不想用
一个。会话
湾TempData的
逻辑
下面的代码是forget password
。用户键入他的电子邮件以重置密码,然后重定向到另一个视图,在那里他可以通过电子邮件收到令牌。现在我希望第一个视图中的emailID作为隐藏字段发送(或者更多),以便我可以发送令牌以及输入的 emailID (隐藏)进行验证第二个视图已发布。
以下是我试过的
FirstMV.cs
public class FirstMV
{
public string Email { get; set; }
}
SecondMV.cs
public class SecondMV
{
public string Token { get; set; }
public string Email { get; set; }
}
FirstView.cshtml
@model FirstMV
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email)
SecondView.cshtml
@model SecondMV
@Html.LabelFor(m => m.Token)
@Html.TextBoxFor(m => m.Token, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Token)
// HERE I WANT TO PUT ONE HIDDEN FIELD MAY BE FOR EMAIL ID
控制器:
[HttpGet]
public ActionResult FirstView()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult ForgotPassword(FirstMV viewModel)
{
// Some code
return RedirectToAction("SecondView", new SecondMV { Email = viewModel.Email });
}
[HttpGet]
public ActionResult SecondView(SecondMV viewModel)
{
return View();
}
[HttpPost]
public ActionResult SecondView(SecondMV viewModel)
{
// Some Code
return View();
}
我收到错误,因为SecondView被定义了两次。
问题1:我实现上述功能的唯一方法是TempData and Session
。我是初学者所以请指导我。如果最佳做法是在我的方案中使用Tempdata and Session
,请告诉我。我会遵循这个。
问题2:我们可以帮助“将匿名对象传递给另一个控制器操作”。
答案 0 :(得分:0)
您遇到的问题是您在第二个操作集上创建了一个重复的签名(在我的示例中为ForgotPasswordCode)。您需要将新模型类型发送到Get for the second set或发送FirstMV,这就是我所展示的。试试这个,看看它是否适合你:
控制器操作:
[HttpGet]
public ActionResult ForgotPassword()
{
Models.FirstMV model = new Models.FirstMV();
return View(model);
}
[HttpPost]
public ActionResult ForgotPassword(Models.FirstMV model)
{
// Do forgot password stuff and redirect
Models.SecondMV secondModel = new Models.SecondMV
{
Email = model.Email,
};
return RedirectToAction("ForgotPasswordCode", "Home", secondModel);
}
[HttpGet]
public ActionResult ForgotPasswordCode(Models.FirstMV model)
{
Models.SecondMV secondModel = new Models.SecondMV
{
Email = model.Email
};
return View(secondModel);
}
[HttpPost]
public ActionResult ForgotPasswordCode(Models.SecondMV model)
{
if (model.Token == "MyTokenValue")
{
return RedirectToAction("ForgotPasswordSuccess", "Home");
}
else
{
return RedirectToAction("ForgotPasswordFailed", "Home");
}
}
[HttpGet]
public ActionResult ForgotPasswordSuccess()
{
return View();
}
[HttpGet]
public ActionResult ForgotPasswordFailed()
{
return View();
}
忘记密码格式
@model WebApplication11.Models.FirstMV
@using (Html.BeginForm("ForgotPassword", "Home", new { Email = Model.Email }, FormMethod.Post))
{
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email)
<button type="submit">Submit</button>
}
忘记密码格式
@model WebApplication11.Models.SecondMV
@using (Html.BeginForm("ForgotPasswordCode", "Home", new { Email = Model.Email, Token = Model.Token }, FormMethod.Post))
{
@Html.LabelFor(m => m.Token)
@Html.TextBoxFor(m => m.Token, new { @class = "form-control" })
@Html.HiddenFor(x => x.Email)
@Html.ValidationMessageFor(m => m.Token)
<button type="submit">Submit</button>
}