是否可以在不使用ASP.NET MVC项目中的参数的情况下RedirectToAction传递数据? [英] Is it possible to RedirectToAction passing data without using parameters in an ASP.NET MVC project?

查看:95
本文介绍了是否可以在不使用ASP.NET MVC项目中的参数的情况下RedirectToAction传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC项目的控制器中,我有一个

In my controller of an ASP.NET MVC project, I have a

return RedirectToAction("CreatePerson", "Home")

此视图是一种可以创建人并且可以正常运行的表单.但是,我想RedirectToAction并用从为系统创建用户的表单中收集的数据预填充表单.

This View is a form that creates a person and that works fine. However, I want to RedirectToAction and pre-fill the form with data collected from a form that creates a User for the system.

我如何在CreatePerson表单中传递来自CreateUser表单的数据?

How would I pass the data from the CreateUser form in the CreatePerson form?

我知道我可以使用参数,但是如果大多数时候我在不需要这些参数的情况下调用CreatePerson视图,这真的是最好的方法.

I know that I could use parameters, but would this really be the best method if most of the time I am calling the CreatePerson view without needing those parameters.

任何朝着正确方向的帮助将不胜感激.

Any help in the right direction would be greatly appreciated.

推荐答案

您不能使用RedirectAction发送数据.那是因为您要进行301重定向,并且要返回到客户端.

You can't send data with a RedirectAction. That's because you're doing a 301 redirection and that goes back to the client.

所以最好使用TempData

假定您将具有以下属性的createperson模型:

Assuming you will have model to createperson with following properties:

public class CreatePersonData
{
     public string name {get; set;}
     public string address {get; set;}
}

现在填充model并存储在TempData

CreatePersonData person=new CreatePersonData();
person.name="SomeName";
person.address="SomeAddress";
TempData["person"]=person;

return RedirectToAction("CreatePerson", "Home")

现在,在接收时只是从tempdata接收它,并将填充的model传递给view

Now while receiving just receive it from tempdata and pass the filled model to the view

public ActionResult CreatePerson()
{
    CreatePersonData person=new CreatePersonData()
    var loadPerson= TempData["person"];
    person = loadPerson;
    return View(person);
}


更新

由于@StephenMuecke用TempData丢失了数据,您可能需要对TempData使用.Keep.Peek来保留将来的请求值

As @StephenMuecke made a point of loosing data with TempData you might need to use .Keep or .Peek with TempData to retain the value for future requests

例如:

.Peek

//PEEK value so it is not deleted at the end of the request
var loadPerson= TempData.Peek("person");

.Keep

//get value marking it from deletion
var loadPerson = TempData["person"];
//later on decide to keep it
TempData.Keep("person");

或@Stephen所说,只需通过id并从数据库

or as @Stephen said just pass the id and select the user from database

例如:

return RedirectToAction("CreatePerson", "Home", new { ID = User.ID });

现在在您的CreatePerson ActionResult中,只需从db中获取它,如下所示:

Now in your CreatePerson ActionResult just get it from db as below:

public ActionResult CreatePerson(int ID)
{
    CreatePersonData person=new CreatePersonData();
    var user=(from u in tbl_user select u where u.ID=ID);
    person.name=user.name;
    person.address=user.address;
    return View(person);
}


更新2

您可以结合使用上述两种方法,例如将数据存储在TempData中以及将IDrouteValues传递,并检查TempData是否不为null,然后回退到使用ID方法检索数据.

You can combine both of the above approaches like storing data in TempData and passing the ID with routeValues and check if TempData isn't null then fallback to retrieval of data using ID approach.

例如:

public class CreatePersonData
{
     public string Id{get; set;}
     public string name {get; set;}
     public string address {get; set;}
}

public ActionResult CreatePerson(int ID)
{
    CreatePersonData person=new CreatePersonData();
    var loadPerson=(CreatePersonData)TempData.Peek("person"); //cast the object from TempData
    if(loadPerson!=null && loadPerson.Id==ID)
    { 
        person=loadPerson;
    }
    else
    {
         var user=(from u in tbl_user select u where u.ID=ID);
         person.name=user.name;
         person.address=user.address;
    }
    return View(person);
}

这篇关于是否可以在不使用ASP.NET MVC项目中的参数的情况下RedirectToAction传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆