0

I am in a learning process and working on ASP.net MVC 5 project. So, I have a model view which has other model views in it.

Parent Model View

public class FullConfigMV
{
 some properties here
 public ElementStyleMV Page { get; set; }
}

Now below is what I want to achieve.

  1. Make an ajax call

  2. once the ajax call hits the controller function set some values

  3. now from there I need to pass that object to another controller action which will return it's view.

Step 1.

<script>
    $(document).ready(function()
    {
        $("#tan").change(function()
        {
            alert(this.value);
            $.ajax({
                type: 'POST',
                url: '/Settings/GetAvailableLocationsFor',
                data: { accountId: 28462, groupId: 35},
                success: function (data) {
                    //Whatever
                },
                error: function () {
                    DisplayError('Failed to load the data.');
                }
            });


        });
    });
</script>

After step 1 my breakpoint hits at

public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
        {
            FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
            Utils.SiteCss = configData.CssStyles();
           // NOW AT THIS PLACE I WANT TO CALL ANOTHER CONTROLLER FUNCTION
           // AND PASS THE `configData Obj`
            return View();
        }

I know we have something like

return RedirectToAction("Index","Home"); 

BUT HOW TO PASS THE config Obj

The controller function that I want to call is in Home Controller and the action name is Index

public ActionResult Index(FullConfigMV data)
        {
            return View();
        }

If the requirement seems weird then kindly bear/humor me on this.

EDIT

After the solution suggested "use TempData " But the problem is I have two index action in my controller. I want the SECOND Index Action to get hit. But the first one is getting hit.

First:

public ActionResult Index()
{
}

Second

[HttpPost]
public ActionResult Index(FullConfigMV data)
{
} 

Code used is

    public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
    {
        FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
        SimUtils.SiteCss = configData.CssStyles();
        TempData["config"] = configData;
        return RedirectToAction("Index", "Home");
    }
12
  • To pass objects between RedirectToAction calls you use Session or TempData. If you just need the (non-view) result from the other controller action -- you move that code into a new class so that any controller can get the result. Commented Jan 17, 2017 at 19:47
  • Please don't use every version tag in your question. You've said you're using version 5; use that version tag. I've edited it for you this time. Commented Jan 17, 2017 at 19:59
  • @MikeMcCaughan : Thanks. I will take care of it from next time. Commented Jan 17, 2017 at 20:01
  • Does your GetAvailableLocations have a view, or is it used to process data? Commented Jan 17, 2017 at 20:02
  • It does not need a view. Just to process data. Commented Jan 17, 2017 at 20:03

3 Answers 3

1

You can achieve this using TempData. From https://msdn.microsoft.com/en-us/library/system.web.mvc.tempdatadictionary(v=vs.118).aspx:

"A typical use for a TempDataDictionary object is to pass data from an action method when it redirects to another action method."

There's plenty of documentation out there but simply use

TempData["config"] = configData;

In your first actionresult, and retrieve it in your Home> Index with

var configData = TempData["config"] as FullConfigMV;
Sign up to request clarification or add additional context in comments.

2 Comments

And then how should I call the Index of homecontroller. return redirectoaction("Index", "home")?
Yes, that will work with TempData, it keeps it for the current and next request.
0

I am not 100% on this, but you might be able to pass that as a route.. if not then use TempData.

Example:

public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
{
    FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
    Utils.SiteCss = configData.CssStyles();
    return RedirectToAction("Index","Home", new { data = configData});
}

Or if above doesn't work:

public ActionResult GetAvailableLocationsFor(int accountId, int groupId)
{
    FullConfigMV configData = SetLoader.GetDSettings(accountId, groupId);
    Utils.SiteCss = configData.CssStyles();
    TempData["config"] = configData;
    return RedirectToAction("Index","Home");
}

// Index ActionResult

public ActionResult Index()
{
    if (TempData["config"] != null)
    {
        FullConfigMV variableName = (FullConfigMV)TempData["config"];
        ...
        return View(/*whatever you want here*/);
    }

    return View();
}

Let me know if that helps

10 Comments

I will try this solution. But I was half way through the other solution that was posted. And I have made the EDIT. Can you see once. I am stuck at one place.
I want to hit the INDEX function which takes the argument. Not the empty one.
Is GetAvailableLocationsFor in the same controller as Index?
Nope. GetAvailableLocationsFor is in settingsController And in home controller I have two action with name as Index. I have added the Edit of return statement of GetAvailableLocationsFor
So, homecontroller has two Index Action.
|
0

For your question After the solution suggested "use TempData " But the problem is I have two index action in my controller. I want the SECOND Index Action to get hit. But the first one is getting hit what you can do is based on some data which you can set in TempData you can call return Index(data) from your first index method, you can get data through Tempdata or some other variable. This will call you second index method through first index method.

1 Comment

I added this ` [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]` on the top of my parameterized index Action which I wanted to hit. And it's working. Thank you so much for your suggestion though..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.