简体   繁体   English

如何在 asp.net MVC 中使用 RedirectToAction 将模型从一种 Action 方法传递到另一种方法? 尽管我发送了一个填充模型,但我得到了空模型

[英]How to pass model from one Action method to another using RedirectToAction in asp.net MVC ? I am getting null model though I send a populated model

I have a class JobSeekerViewModel.我有一个 JobSeekerViewModel 类。 I want to Send it from Action method Index to Details.我想将它从操作方法索引发送到详细信息。 but whenever I send I get null values.但是每当我发送时,我都会得到空值。 but my model is populated.但我的模型已填充。

public class JobSeekerViewModel
{
    public user userJobSeeker { get; set; }

    public employee employeeJobSeeker { get; set; }
       }
}

index(login log1)
{
    Return View(log1);
}


[HttpPost]
index (login log1) 
{
    JobSeekerViewModel js = new JobSeekerViewModel();
    js.userJobSeeker = log1.userJobSeeker;
    js.employeeJobSeeker = log1.employeeJobSeeker;
    return Details(js);
}
Details(JobSeekeerViewModel js)
{
     Return View(js);
}

Index索引

        TempData["jobSeeker"] = jobSeeker;

Details详情

        JobSeekerViewModel jobSeeker = (JobSeekerViewModel)TempData["jobSeeker"];

try this instead试试这个

index (JobSeekerViewModel js) 
{
   return RedirectToAction("Details", new{model = js});
}

This may help you, I hope you get the idea.这可能对你有帮助,我希望你能明白。

Index (login log1) 
{
  //get your JobSeekerViewModel and then use it.

   return RedirectToAction("Details", new { model = jobSeekerViewModel });
}

Details(JobSeekerViewModel model) 
{
   return View(model);
}

When you use redirectToAction the data is not preserved between the controller and the view and hence you get null当您使用redirectToAction ,数据不会保留在控制器和视图之间,因此您会得到null

Either use TempData or call the method details() directly instead of redirectToAction要么使用TempData要么直接调用方法details()而不是redirectToAction

Read this for examples : Can we pass model as a parameter in RedirectToAction?阅读此示例: 我们可以在 RedirectToAction 中将模型作为参数传递吗?

if your action is in the same controller you don't need to call RedirectToAction .如果您的操作在同一个控制器中,则不需要调用RedirectToAction

simply call action like:只需调用如下操作:

index (login log1) 
{
    JobSeekerViewModel js = new JobSeekerViewModel();
    js.userJobSeeker = log1.userJobSeeker;
    js.employeeJobSeeker = log1.employeeJobSeeker;
    return Details(js);
}

To Send model from one action to another action -将模型从一个动作发送到另一个动作 -

In Action to set model:在行动中设置模型:

TempData["myId"] = myIdToTransfer;

In another Action get model:在另一个 Action get 模型中:

String userID = TempData["myID"].ToString;

To send model from an action to view将模型从操作发送到查看

In action to set model:在行动中设置模型:

ViewBag.myId = myIdToTransfer;

In View to get model:在 View 中获取模型:

@Html.ViewBag.myId

暂无
暂无

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

相关问题 如何使用 C# 在 ASP.NET 核心 MVC 中将 model 从一个动作传递到另一个动作而不使用 TempData - How to pass model from one action to another without using TempData in ASP.NET Core MVC using C# 将模型从一个动作传递到另一个asp.net mvc - Passing model from one action to another asp.net mvc C#-如何在ASP.NET MVC的HTML表单中传递内部带有其他模型的模型 - C# - How do I pass a model with another model inside from a HTML form in ASP.NET MVC ASP.net MVC2。 填充的模型没有回到控制器 - ASP.net MVC2. Populated model is not getting back to the controller 如何在ASP.NET MVC 3中为填充的下拉列表创建视图模型 - How do I create a view model for a populated drop down list in ASP.NET MVC 3 从 ASP.NET MVC 迁移到 ASP.NET 核心 MVC - 操作 model 绑定导致 Z37A6259CC6491DAE299A8D - Migration from ASP.NET MVC to ASP.NET Core MVC - action model binding resulting null 如何在ASP.NET MVC的父域模型中将此子模型引用传递给虚拟ICollection? - How do I pass this child model reference to a virtual ICollection in parent domain model in ASP.NET MVC? ASP.NET MVC:如何将列表(从Model中的类)传递到View中的转发器? - ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View? ASP.NET-MVC-从另一个模型获取数据 - ASP.NET - MVC - Getting data back from another Model 我如何将现有模型(设备)数据附加到MVC ASP.NET中的另一个模型(用户)数据 - How can i attach existing model(device) data to another model(user) data in mvc asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM