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.