Using RedirectToAction with a Custom Type Parameter

asp.net mvc 2

I have this action in the identity controller

public ActionResult Details(string id, MessageUi message)
{

And I'm trying to redirect this action from another controller, but I don't know how to pass the message parameter

I tried with

    var id = "someidvalue"
    var message = new MessageUi("somevalue");
    return RedirectToAction("Details", "Identity", new { id, message});
}

but the message parameter is null

+3
source share
1 answer

This is normal. You cannot pass complex objects in URLs when redirecting and that is the reason the object was MessageUinot received. Only scalar properties to be translated into &-separated key / value pairs in the URL.


- , :

var id = "someidvalue"
var message = new MessageUi("somevalue");
return RedirectToAction("Details", "Identity", new { 
    id = id, 
    MessageProp1 = message.MessageProp1,
    MessageProp2 = message.MessageProp2,
    MessageProp3 = message.MessageProp3,
});

:

var id = "someidvalue";
return RedirectToAction("Details", "Identity", new { 
    id = id, 
    messageId = "somevalue"
});

id:

public ActionResult Details(string id, string messageId)
{
    var message = new MessageUi(messageId);
    ...
}

MessageUi.


- TempData Session:

var id = "someidvalue";
TempData["message"] = new MessageUi("somevalue");
return RedirectToAction("Details", "Identity", new { id });

Details:

var message = TempData["message"] as MessageUi;
+8

Source: https://habr.com/ru/post/1794341/


All Articles