是否可以在不使用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
中以及将ID
与routeValues
传递,并检查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);
}