简体   繁体   中英

Passing an model from one controller to another controller in MVC3 not working

I am passing an model from one controller method to another controller method to collect all the values to model fields. I am doing as shown below

[HttpPost]
    public ActionResult A(Model m, string s)
    {
        if (ModelState.IsValid) 
        {
            if (m.l == null || m.k == null)
            {
                //Do something.

            }
            else
                return View("B", m); // this is where by debug point comes...
        }

        return View(m);


    }


public ActionResult B(Model m)
    {

        return View(m);
    }

[HttpPost]
    public ActionResult B(Model m, string s)
    {
        if (ModelState.IsValid)
        {
            if (m.p == null || m.j == null)
            {
                //do something
            }
            else
            {
               // do something and redirect to somewhere else 
            }


        }

But as I have shown the debug point comes here as shown below.

return View("B", m);

This should hit the Controller method "B" But the problem is It does not hit the controller method "B". But it shows the view for the Controller method "B" So, I am confused about this problem. And I can not see the values for (l,k) in the httppost of method "B". What is the reason for this.

I want to know that, I am doing this right or wrong. If I am doing this wrong can you please explain on this for a bit. Can someone who is good at MVC help me. Thanks in advance.

That code -- return View("B", m) -- actually doesn't redirect to the action B, it just renders the view B with the given model. If you want to execute the action, then you should use:

return B(m);

You should not call an action from another action. The view RESULT is executed after the controllers action method (the top one) is executed . Calling on action from another is not intended and can provide multiple pathways to the same action method. Your controller purpose is to orchestrate data. If you think multiple methods should be called then you should break those apart into other facade classes or service layer classes that each controller that needs it will request it or aggregate this information into a domain object that you then populate a view model from

If any of this isn't clear I'll clarify further.

Either call several repository methods to get the data and populate into a view model Or Use a parent view and call RenderAction from within the view to emit other controllers data

Or use RenderPartial to use your parent views object to pass to each child view to render it's portion but I like the RenderAction more unless all the data makes sense to be available to your top level view's view model. Or Call a facade layer class from your controller that in turn gets data from various locations, does xyz and returns it to the controller who then populates a view model from this data.

Try something like this,

[HttpPost]
public ActionResult A(Model m, string s)
{
    if (ModelState.IsValid) 
    {
        if (m.l == null || m.k == null)
        {
            //Do something.                    
        }
        else
            RedirectToAction("B", m); // check this
    }
    return View(m);    
}   

public ActionResult B(Model model)
{
    return View(model);
}

Try the below code. It should work. This is an alternative to the answer of @dbaseman

    [HttpPost]
    public ActionResult A(Model m, string s)
    {
        if (ModelState.IsValid) 
        {
            if (m.l == null || m.k == null)
            {
                //Do something.                    
            }
            else
            {
                TempData["tempModel"]=m; //cannot pass a model in a redirect method. so store it in a tempdata object.
                return RedirectToAction("B"); // redirect to action method B
            }
        }
        return View(m);    
    }   

    public ActionResult B()
    {
        Model model= new Model();
        if(TempData["tempModel"]!=null)
            model=(Model)TempData["tempModel"];
        return View(model);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM