我们可以将模型作为重定向作用的参数传递吗?

时间:2021-07-15 16:28:43

I want to know, there is any technique so we can pass Model as a parameter in RedirectToAction

我想知道的是,有任何技术,所以我们可以通过模型作为一个参数在RedirectToAction中。

For Example:

例如:

public class Student{
    public int Id{get;set;}
    public string Name{get;set;}
}

Controller

控制器

public class StudentController : Controller
{
    public ActionResult FillStudent()
    {
        return View();
    }
    [HttpPost]
    public ActionResult FillStudent(Student student1)
    {
        return RedirectToAction("GetStudent","Student",new{student=student1});
    }
    public ActionResult GetStudent(Student student)
    {
        return View();
    }
}

My Question - Can I pass student model in RedirectToAction?

我的问题是——我能通过学生模型吗?

5 个解决方案

#1


49  

Using TempData

使用TempData

Represents a set of data that persists only from one request to the next

表示一组只从一个请求保存到下一个请求的数据

[HttpPost]
public ActionResult FillStudent(Student student1)
{
    TempData["student"]= new Student();
    return RedirectToAction("GetStudent","Student");
}

[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
    Student std=(Student)TempData["student"];
    return View();
}

Alternative way Pass the data using Query string

使用查询字符串传递数据的替代方法

return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});

This will generate a GET Request like Student/GetStudent?Name=John & Class=clsz

这会生成一个像Student/GetStudent这样的GET请求吗?Name = = clsz约翰和类

Ensure the method you want to redirect to is decorated with [HttpGet] as the above RedirectToAction will issue GET Request with http status code 302 Found (common way of performing url redirect)

确保您要重定向到的方法被修饰为[HttpGet],因为上面的RedirectToAction将使用找到的http状态代码302发出GET请求(执行url重定向的常用方法)

#2


23  

Just call the action no need for redirect to action or the new keyword for model.

只需调用动作,不需要重定向到动作,也不需要调用model的new关键字。

 [HttpPost]
    public ActionResult FillStudent(Student student1)
    {
        return GetStudent(student1); //this will also work
    }
    public ActionResult GetStudent(Student student)
    {
        return View(student);
    }

#3


8  

Yes you can pass the model that you have shown using

是的,您可以传递您所展示的使用的模型

return RedirectToAction("GetStudent", "Student", student1 );

assuming student1 is an instance of Student

假设学生1是学生的一个实例。

which will generate the following url (assuming your using the default routes and the value of student1 are ID=4 and Name="Amit")

这将生成以下url(假设您使用默认路由,且student1的值为ID=4, Name="Amit")

.../Student/GetStudent/4?Name=Amit

…/学生/ GetStudent / 4 ?名字=阿米特

Internally the RedirectToAction() method builds a RouteValueDictionary by using the .ToString() value of each property in the model. However, binding will only work if all the properties in the model are simple properties and it fails if any properties are complex objects or collections because the method does not use recursion. If for example, Student contained a property List<string> Subjects, then that property would result in a query string value of

在内部,RedirectToAction()方法使用模型中每个属性的. tostring()值构建一个RouteValueDictionary。但是,如果模型中的所有属性都是简单的属性,那么绑定只会起作用,如果任何属性是复杂对象或集合,则会失败,因为该方法不使用递归。例如,如果Student包含一个属性列表 subject,那么该属性将导致查询字符串值为

....&Subjects=System.Collections.Generic.List'1[System.String]

....主题= System.Collections.Generic.List ' 1 system . string][

and binding would fail and that property would be null

绑定会失败,这个属性会为空

#4


0  

  [HttpPost]
    public async Task<ActionResult> Capture(string imageData)
    {                      
        if (imageData.Length > 0)
        {
            var imageBytes = Convert.FromBase64String(imageData);
            using (var stream = new MemoryStream(imageBytes))
            {
                var result = (JsonResult)await IdentifyFace(stream);
                var serializer = new JavaScriptSerializer();
                var faceRecon = serializer.Deserialize<FaceIdentity>(serializer.Serialize(result.Data));

                if (faceRecon.Success) return RedirectToAction("Index", "Auth", new { param = serializer.Serialize(result.Data) });

            }
        }

        return Json(new { success = false, responseText = "Der opstod en fejl - Intet billede, manglede data." }, JsonRequestBehavior.AllowGet);

    }


// GET: Auth
    [HttpGet]
    public ActionResult Index(string param)
    {
        var serializer = new JavaScriptSerializer();
        var faceRecon = serializer.Deserialize<FaceIdentity>(param);


        return View(faceRecon);
    }

#5


-3  

i did find something like this, helps get rid of hardcoded tempdata tags

我确实找到了这样的东西,帮助去掉硬编码的tempdata标签

public class AccountController : Controller
{
    [HttpGet]
    public ActionResult Index(IndexPresentationModel model)
    {
        return View(model);
    }

    [HttpPost]
    public ActionResult Save(SaveUpdateModel model)
    {
        // save the information

        var presentationModel = new IndexPresentationModel();

        presentationModel.Message = model.Message;

        return this.RedirectToAction(c => c.Index(presentationModel));
    }
}

#1


49  

Using TempData

使用TempData

Represents a set of data that persists only from one request to the next

表示一组只从一个请求保存到下一个请求的数据

[HttpPost]
public ActionResult FillStudent(Student student1)
{
    TempData["student"]= new Student();
    return RedirectToAction("GetStudent","Student");
}

[HttpGet]
public ActionResult GetStudent(Student passedStd)
{
    Student std=(Student)TempData["student"];
    return View();
}

Alternative way Pass the data using Query string

使用查询字符串传递数据的替代方法

return RedirectToAction("GetStudent","Student", new {Name="John", Class="clsz"});

This will generate a GET Request like Student/GetStudent?Name=John & Class=clsz

这会生成一个像Student/GetStudent这样的GET请求吗?Name = = clsz约翰和类

Ensure the method you want to redirect to is decorated with [HttpGet] as the above RedirectToAction will issue GET Request with http status code 302 Found (common way of performing url redirect)

确保您要重定向到的方法被修饰为[HttpGet],因为上面的RedirectToAction将使用找到的http状态代码302发出GET请求(执行url重定向的常用方法)

#2


23  

Just call the action no need for redirect to action or the new keyword for model.

只需调用动作,不需要重定向到动作,也不需要调用model的new关键字。

 [HttpPost]
    public ActionResult FillStudent(Student student1)
    {
        return GetStudent(student1); //this will also work
    }
    public ActionResult GetStudent(Student student)
    {
        return View(student);
    }

#3


8  

Yes you can pass the model that you have shown using

是的,您可以传递您所展示的使用的模型

return RedirectToAction("GetStudent", "Student", student1 );

assuming student1 is an instance of Student

假设学生1是学生的一个实例。

which will generate the following url (assuming your using the default routes and the value of student1 are ID=4 and Name="Amit")

这将生成以下url(假设您使用默认路由,且student1的值为ID=4, Name="Amit")

.../Student/GetStudent/4?Name=Amit

…/学生/ GetStudent / 4 ?名字=阿米特

Internally the RedirectToAction() method builds a RouteValueDictionary by using the .ToString() value of each property in the model. However, binding will only work if all the properties in the model are simple properties and it fails if any properties are complex objects or collections because the method does not use recursion. If for example, Student contained a property List<string> Subjects, then that property would result in a query string value of

在内部,RedirectToAction()方法使用模型中每个属性的. tostring()值构建一个RouteValueDictionary。但是,如果模型中的所有属性都是简单的属性,那么绑定只会起作用,如果任何属性是复杂对象或集合,则会失败,因为该方法不使用递归。例如,如果Student包含一个属性列表 subject,那么该属性将导致查询字符串值为

....&Subjects=System.Collections.Generic.List'1[System.String]

....主题= System.Collections.Generic.List ' 1 system . string][

and binding would fail and that property would be null

绑定会失败,这个属性会为空

#4


0  

  [HttpPost]
    public async Task<ActionResult> Capture(string imageData)
    {                      
        if (imageData.Length > 0)
        {
            var imageBytes = Convert.FromBase64String(imageData);
            using (var stream = new MemoryStream(imageBytes))
            {
                var result = (JsonResult)await IdentifyFace(stream);
                var serializer = new JavaScriptSerializer();
                var faceRecon = serializer.Deserialize<FaceIdentity>(serializer.Serialize(result.Data));

                if (faceRecon.Success) return RedirectToAction("Index", "Auth", new { param = serializer.Serialize(result.Data) });

            }
        }

        return Json(new { success = false, responseText = "Der opstod en fejl - Intet billede, manglede data." }, JsonRequestBehavior.AllowGet);

    }


// GET: Auth
    [HttpGet]
    public ActionResult Index(string param)
    {
        var serializer = new JavaScriptSerializer();
        var faceRecon = serializer.Deserialize<FaceIdentity>(param);


        return View(faceRecon);
    }

#5


-3  

i did find something like this, helps get rid of hardcoded tempdata tags

我确实找到了这样的东西,帮助去掉硬编码的tempdata标签

public class AccountController : Controller
{
    [HttpGet]
    public ActionResult Index(IndexPresentationModel model)
    {
        return View(model);
    }

    [HttpPost]
    public ActionResult Save(SaveUpdateModel model)
    {
        // save the information

        var presentationModel = new IndexPresentationModel();

        presentationModel.Message = model.Message;

        return this.RedirectToAction(c => c.Index(presentationModel));
    }
}