C# 在ASP.NETMVC5中,将数据从一个控制器操作传递到另一个控制器操作

C# 在ASP.NETMVC5中,将数据从一个控制器操作传递到另一个控制器操作,c#,asp.net,asp.net-mvc,razor,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Razor,Asp.net Mvc 5,我需要将数据从一个控制器动作传递到另一个控制器动作。 我不想用 a。会议 b。临时数据 逻辑 下面的代码是关于忘记密码。用户键入他的电子邮件进行密码重置,然后被重定向到另一个视图,在那里他可以放置通过电子邮件收到的令牌。现在,我希望第一个视图中的emailID作为隐藏字段(或任何内容)发送,这样我就可以发送令牌以及在发布第二个视图时输入以进行验证的emailID(隐藏)。 下面是我尝试过的 FirstMV.cs public class FirstMV { public string E

我需要将数据从一个控制器动作传递到另一个控制器动作。 我不想用

a。会议

b。临时数据

逻辑

下面的代码是关于
忘记密码
。用户键入他的电子邮件进行密码重置,然后被重定向到另一个视图,在那里他可以放置通过电子邮件收到的令牌。现在,我希望第一个视图中的emailID作为隐藏字段(或任何内容)发送,这样我就可以发送令牌以及在发布第二个视图时输入以进行验证的emailID(隐藏)。 下面是我尝试过的

FirstMV.cs

public class FirstMV
{
    public string Email { get; set; }
}
public class SecondMV
{
    public string Token { get; set; }
    public string Email { get; set; }
}
SecondMV.cs

public class FirstMV
{
    public string Email { get; set; }
}
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)
@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
SecondView.cshtml

@model FirstMV
@Html.LabelFor(m => m.Email)
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Email)
@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();
}
[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();
    }
由于第二个视图定义了两次,因此出现了错误

问题1:我实现上述功能的唯一方法是
TempData和Session
。我是初学者,请引导我。如果最佳实践是在我的场景中使用
Tempdata和Session
,请告诉我。我会遵守的


问题2:我们能否帮助“传递匿名对象”到另一个控制器操作。

您遇到的问题是,您在第二个操作集上创建了重复的签名(在我的示例中,放弃了PasswordCode)。您要么需要将新的模型类型发送到第二组的Get中,要么发送第一个MV,这就是我所展示的。试试这个,看看它是否适合你:

控制器操作:

[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();
}
[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>
}
@model WebApplication11.Models.FirstMV
@使用(Html.BeginForm(“ForgotPassword”,“Home”,new{Email=Model.Email},FormMethod.Post))
{
@LabelFor(m=>m.Email)
@TextBoxFor(m=>m.Email,新的{@class=“form control”})
@Html.ValidationMessageFor(m=>m.Email)
提交
}
忘记密码表单

@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>
}
@model WebApplication11.Models.SecondMV
@使用(Html.BeginForm(“ForgotPasswordCode”,“Home”,新的{Email=Model.Email,Token=Model.Token},FormMethod.Post))
{
@LabelFor(m=>m.Token)
@TextBoxFor(m=>m.Token,新的{@class=“form control”})
@Html.HiddenFor(x=>x.Email)
@Html.ValidationMessageFor(m=>m.Token)
提交
}

以前从未真正见过[HTTPGet]的使用。尝试使用ViewBags在视图和控制器之间来回传递数据。对于隐藏信息,请尝试@Html.HiddenFor(x=>x.EmailId)尝试返回重定向到操作(“SecondView”,new{EmailId=EmailId,message=”此人没有电子邮件“});在MVC中,message是ViewBag的名称,使用HttpGet和HttpPost将允许您有两个相同名称的操作方法…一个用于进入页面,另一个用于离开页面…请参见下面的示例。它允许你准确地发送OP询问的内容。通常,我会建议远离ViewBag,除非您能提供其他帮助……如果使用MVC,请坚持使用强类型模型……非常强大。您的
SecondView()
GET方法需要
返回视图(viewModel)
和视图中的
@Html.HiddenFor(m=>m.Email)
。但是GET方法的签名需要是
public ActionResult SecondView(string email)
,并且在方法
return RedirectToAction(“ForgotPasswordCode”,“Home”,secondModel)中初始化一个新的
SecondMV
您正在httppost方法中的
returnredirect
中返回SecondModel的实例。但是在SecondView conroller操作的httpget中,您将其存储在参数
Models.FirstMV model
secondModel
类型的返回如何存储并被
model.FirstMV model
接受这就是我所说的意思,“你要么需要在Get中输入第二套的新型号,要么输入第一套MV,这就是我所展示的“。最佳做法是创建一个新模型,将其发送到HttpGet以获取ForgotPasswordCode,但我只是重新使用了FirstMV,因为它上面已经有电子邮件,这就是我在示例中关心的全部内容。如果您的模型名称具有更好的语义含义,那么这可能也不会如此混乱。碰巧MVC为我很好地处理了强制,但我同意这样写似乎有点奇怪。嗯。。。