1

I have a problem with "return view()" that doesn't excute code in my controller.

I have a controller with this code:

public class BrokerController : BaseController
{

    public ActionResult BestallMaklarBild()
    {
        return View(new BestallMaklarBildViewModel());
    }

    [HttpPost]
    public ActionResult BestallMaklarBild(FormCollection collection)
    {
        try
        {
            //Some code                
            return View("MaklarBildBestalld",new MaklarBildBestalldViewModel());
        }
        catch
        {
            return View(new BestallMaklarBildViewModel());
        }
    }

    public ActionResult MaklarBildBestalld()
    {
       //Some code     
        return View(new MaklarBildBestalldViewModel());
    }
}

When I post to my controller "BestallMaklarBild" and then try to return the view("MaklarBildBestalld",new MaklarBildBestalldViewModel()), the code in "MaklarbildBestalld" doesn't execute. So when the model is returned to the view it doesn't contain any data, and cause my view leaving me with an error saying that the model.something = null when I try to output some data to the view.

Don't know what Im doing wrong.

2
  • 3
    Have you noticed you are not assigning anything to model.something ? Commented Aug 18, 2012 at 13:05
  • 2
    Darin is correct. Why would you expect "return View();" to execute a controller method instead of, you know, rendering a view? MVC kind of implies that the V and C parts are separate... :-) Commented Aug 18, 2012 at 15:04

2 Answers 2

2

That's normal. You need to redirect to this controller action if you want the code to execute:

try
{
    //Some code                
    return RedirectToAction("MaklarBildBestalld", new { id = "some id" });
}
catch
{
    return View(new BestallMaklarBildViewModel());
}

and now the action will execute:

public ActionResult MaklarBildBestalld(string id)
{
    //Some code     
    return View(new MaklarBildBestalldViewModel());
}

Here's the correct workflow:

  1. The BestallMaklarBild POST controller action is invoked and passed a view model
  2. This controller action attempts to persist the model and redirects in case of success to the MaklarBildBestalld controller action passing it an unique identifier as query string of this model so that this action is able to retrieve the model back. In case of failure it simply redisplays the form so that the user can fix the errors he did, or see the error message that there was a problem processing his request.
Sign up to request clarification or add additional context in comments.

2 Comments

So I have to add an inputparameter to my MaklarBildBestalld() ActionResult to get this to Work? Today i don't have any input parameter. I could't find any RedirectToActionView, but i surpose you mean RedirectToAction method. Sorry but I'm a newbie to MVC.
Yes, that was a type. The correct method to use is RedirectToAction. And you could pass the id parameter in order to be able to retrieve the model in the target action using this id.
0

You must ensure that your view has the model in your parameters as a generic type.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.