Post data from one controller to another - c#

I have a mvc application. I want to post 2 string from my controller to another controller in controller. Is it possible?

You can target any controller action from your form input in order to POST a string. If you want to pass a string or object from controller to controller you can use sessions or alternatively a database.
Additionally if you want to pass strings or objects between action methods in the same controller you can use the TempData collection as well.

Yes, You can use TempData for that like...
public ActionResult Sample1()
{
TempData["Test"] = "Test1"
return RedirectToAction("Sample2");
}
public ActionResult Sample2()
{
var test= TempData["Test"] as string
return View( test);
}

Related

ASP.NET Core passing an object to a view without passing it as a parameter in the action method

I want to create an edit form , when it autofills the properties of the object i use , but i want it to be an httpGet method so i have the httpPost method with the same name and there i'll submit the data to db. i can't use the same type object
What should i do?
[HttpGet]
public IActionResult EditMessage(MessageModel model)
{
return View(model);
}
[HttpPost]
public IActionResult EditMessage(MessageModel newModel)
{
newModel.Update(_context);
return View("Messages");
}
Okay so i found out how to resolve my problem.
ill just take the id of the object and pass that to the method.
in the method ill find what object has that id and pass that object to the view :)

How to RedirectToAction with a model passed?

I have an model class that is used to validate some user input.
I have an controller with the following.
public IActionResult Checkout(GiftCard giftCard)
{
}
I was wondering how I could on an different action redirect it back to it such as
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout");
}
The above doesn't work because asp.net is trying to find an action without the model like the one below
public IActionResult Checkout()
{
}
if your url going to be really long make a shorturl so load that and redirect from there, if you use ajax it wont be visible
You could use action with another name and apply action selector to your renamed method. Like next:
[ActionName("Checkout")]
[HttpPost] //Recomend you send user input via post
[ValidateAntiForgeryToken] // and use validation token
public IActionResult CheckoutConfirmed(GiftCard giftCard)
{
//your code
}
public IActionResult Checkout()
{
//your code
}
Check out more about ASP.NET MVC - Selectors
If you need more information about ValidateAntiForgeryToken, you could find it there - Chapter 12: Security
And also, you could find great article about posting there - ASP.NET MVC Preview 5 and Form Posting Scenarios
RedirectToAction has a second parameter called routeValues with which you can pass the GiftCard like following.
public IActionResult Preview(GiftCard giftCard)
{
return RedirectToAction("Checkout", giftCard);
}

How to safely pass modelinfo between different actions in MVC?

I've got a certain action in my controller.
I've created a view for it and linked a model in it... now I want to pass some info from a property from this model onto another action safely... Only ways I know are to pass it with #Html.ActionLink /with a hidden field in a form.
But these aren't secure at all as far as I know... so what other way is there to do this ?
You can store it in a session variable:
To store a property of the model in the first action:
public ViewActionResult SomeAction(SomeModel model)
{
Session["remember"] = model.someProperty;
return View();
}
To retrieve it in another action:
public ViewResult SomeOtherAction()
{
var rememberedValue = Session["remember"];
return View();
}

MVC passing complex object to action

I have two actions in my controller when a user call one i need to redirect it to another one and pass a complex object :
first action :
public virtual ActionResult Index(string Id) {
var input = new CustomInput();
input.PaymentTypeId = Id;
return RedirectToAction(MVC.Ops.SPS.Actions.Test(input));
}
second action :
public virtual ActionResult Test(CustomInput input) {
return View();
}
The probelm is that the input arrives null at the second action. how can i solve it?
You can solve this using temp data to temporarily hold a value from which the second method retrieves that value.
public virtual ActionResult Index(string Id)
{
var input = new CustomInput();
input.PaymentTypeId = Id;
TempData["TheCustomData"] = input; //temp data, this only sticks around for one "postback"
return RedirectToAction(MVC.Ops.SPS.Actions.Test());
}
public virtual ActionResult Test()
{
CustomInput = TempData["TheCustomData"] as CustomInput;
//now do what you want with Custom Input
return View();
}
You can keep your tempData going so long as it is never null using the .keep() method like this,
if (TempData["TheCustomData"] != null)
TempData.Keep("TheCustomData");
I want to make sure that you know that RedirectToAction creates new HTTP request so this create another GET and all you can pass is RouteValueDictionary object which is like having query string parameters which is list of key and value pairs of string. That said, You can't pass complex object with your way of code however the TempData solution mentioned by #kyleT will work.
My recommendation based on your code is to avoid having two actions and redirect from one another unless you are doing something more than what you have mentioned in your question. Or you can make your Test action accepting the id parameter the your Index action will contain only RedirectToAction passing the id as route parameter.
Edit:
Also, If you have no primitive properties you can pass your object like the following (Thanks to #Stephen Muecke)
return RedirectToAction("Test",(input);

passing model to RedirectToAction without using session or tempdata?

I m modifying an existing code.
From one action I use RedirectToAction to transfer execution control to another action. I need to pass Model with RedirectToAction as well. My idea is it can't be done directly by passing Model to 2nd action without using Session or tempData. But still want to ask is there a technique to pass model with RedirectToAction ? I don't want to put Model in Session or TempData.
Thanks
You can try something like that, but it doesn't feel like a natural action:
public ActionResult Index()
{
return RedirectToAction("AnotherAction", new
{
Parameter1 = Parameter1,
Parameter2 = Parameter2,
});
}
[HttpGet]
public ActionResult AnotherAction(ModelClass model)
{
//model.Parameter1
//model.Parameter2
return View(model);
}
I would expect the code of the previous answer to throw an exception when attempting to implicitly convert an object of parameter1 and parameter2 to ModelClass.
That being said, The best way is to just pass the id of the entity to your new action, then access your repository to initialize your model with the id that has been passed. Lets assume you have initialized a user with a UserID property.
return RedirectToAction("NextAction", new { id = user.UserID });
Then in NextAction just initialize the model with the passed id

Categories