niico
niico

Reputation: 12749

Is returning different view with object bad practice in ASP.net MVC 5?

I need to pass objects between ActionMethods and Views in an ASP.net MVC 5 app.

I'm using it for a multi page signup - and for a multi page payment.

Is this bad practice? I haven't seen a good way to pass objects between different controllers.

Code:

public ActionResult Join1()
{
    //
    return View("Join2", MyObject);
}

[HttpPost]
public ActionResult Join2(MyObject MyObj)
{
    //manipulate object
    //return
}

It seems to be an effective way to do it - though I haven't seen many people do it this way. I haven't seen objects being passed between action methods much at all.

Is there a flaw in this approach, a better way of passing models between Views - or should each ActionMethod stick to passing simple data with, say, TempData instead of objects?

Why haven't I seen any sample projects doing things like this?

I've seen return RedirectToAction("act"); plenty - but that is Get and passing an object in a URI is limiting - and I don't want users to be able to manipulate or see the data being passed.

thx.

Upvotes: 0

Views: 112

Answers (1)

Daniel Hume
Daniel Hume

Reputation: 447

Unless I have misunderstood the description, your code is not doing what you think it's doing. That first return View("Join2", MyObject); statement is not executing the second ActionMethod, it is only passing your data into the View that happens to have the same name as the second method. Therefore the code implied by //manipulate object will not run before that View is rendered and sent back to the user.

Assuming the View file Join2.cshtml exists, and it contains the default @using (Html.BeginForm()), then users submitting the form will cause the Join2 Action to be executed and the same view rendered with the manipulated data - unless, of course, you add another return View() statement that names a different View.

The reason you haven't seen this done much is that the MVC convention is to have a View named the same as the ActionMethod, this makes the code slightly simpler and also much easier for other ASP.NET developers to understand because it is what they are expecting to see.

If you want the form rendered by each View to then execute a different ActionMethod when it is posted back, the place to do that is in the View code, where Html.BeginForm() has several overloads that allow you to do just that, e.g. in Join.cshtml you could write:

@using (Html.BeginForm("Join2", "JoinController"))
{
    // form fields and stuff
}
// Produces the following form element
// <form action="/JoinController/Join2" action="post">

To address the final part of your question, "I don't want users to be able to manipulate or see the data being passed", sorry to say it but your proposed code doesn't prevent that: users can see the data in the web form, before it is ever posted back to the Join2 method; they can manipulate the data by sending an HTTP POST containing any data they want back to the Join2 method.

If you absolutely, positively need to actually execute Join2() from within Join(), before anything is passed back to the user, then you can call it just like any other C# method:

var myResult = Join2(MyObject);

Then you have an ActionResult object that you can manipulate or return straight to the browser. But why you would want to do this, is beyond me.

Upvotes: 1

Related Questions