将数据从一个控制器动作传递到另一个控制器动作在ASP.Net MVC 5
我需要将数据从一个控制器动作传递给另一个。 我不想用将数据从一个控制器动作传递到另一个控制器动作在ASP.Net MVC 5
a。 Session
b。 TempData的
逻辑
下面代码是关于forget password
。用户键入他的电子邮件密码重置,然后重定向到另一个视图,他可以通过电子邮件接收令牌。现在我想从第一个视图发送emailID作为隐藏字段(或WHATEVER),这样我可以发送令牌以及电子邮件ID(隐藏),以便在发布第二个视图时进行验证。 下面好吧是我曾尝试
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:我们可以帮助“传递匿名对象”到另一个控制器动作。
您遇到的问题是您已在第二个操作集(在我的示例中为ForgotPasswordCode)上创建了重复签名。您需要将新模型类型发送到第二组获取中,或者发送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();
}
密码忘FORM
@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>
}
中初始化新的'SecondMV''返回RedirectToAction(“ForgotPasswordCode”,“Home”,secondModel);'您正在返回在httppost方法中'returnredirect'中的SecondModel实例。但是在用于SecondView控制器操作的httpget中,您将其存储在参数'Models.FirstMV模型中' – Unbreakable
'Model.FirstMV模型'可如何存储和接受'secondModel'类型的返回值? – Unbreakable
这就是我的意思说:“你要么需要发送一个新的模型类型到第二组的Get中,或者发送FirstMV,这正是我所展示的。”最好的做法是创建一个新的模型发送到HttpGet的ForgotPasswordCode,但我刚刚重新使用FirstMV,因为它已经有了电子邮件,这就是我所关心的例子。如果你的模型名称具有更好的语义含义,那么这可能不会造成混淆。这恰好是MVC很好地处理了我的强制问题,但我同意这样写就显得有点奇怪。 HTH ... – user1011627
以前从未真正看到过使用[HTTPGet]。尝试使用ViewBags将数据从视图传递到控制器。对于隐藏信息,请尝试@ Html.HiddenFor(x => x.EmailId)尝试返回RedirectToAction(“SecondView”,new {EmailId = EmailId,message =“此人没有电子邮件。”});其中消息是视图包的名称 – Edward
在MVC中,使用HttpGet和HttpPost将允许您拥有2个相同名称的Action方法......一个用于访问页面,然后一个用于跳出页面....看到我下面的例子。它可以让你准确地发送OP所询问的内容。通常情况下,我会建议远离ViewBag,除非你可以帮助它......如果使用MVC,坚持使用强类型模型....非常强大。 – user1011627
您的'SecondView()'GET方法需要'返回View(viewModel);'并在视图中@@ Html.HiddenFor(m => m.Email)'。但GET方法的签名需要为'public ActionResult SecondView(字符串电子邮件)',并且您在方法 –