I am looking out for different alternatives of passing parameter from an action method in JSF Managed bean to next view.
For example, I have an action method in my managed bean.
public String actionMethod01(){
String outcome = "nextPage";
return outcome;
}
If I want to pass a parameter to nextPage, one option which I have is:
outcome += "?param1=value1";
But, its not so convinient if I have multiple parameters to be passed.
Is there a better way for doing it?
Best regards,
Anand.
There's nothing in the JSF API which makes this easier. Just create a helper/utility method yourself which makes it more convenient so that you can end up with something like this:
return "nextPage" + toQueryString("param1", "value1", "param2", "value2");
Related
I have such action:
[ObjectRequired]
public ActionResult Campaign(int? id, SomeClass object = null)
{
...
}
What i need is to route to this action:
a) with only int parameter (.../Campaign/12345)
b) with no parametes (optionally) (.../Campaign)
MVC error says, that there is no nonparametric constructor (if delete "object" parameter - it's ok). But i cant delete "object" parameter, because i need to check some values and pass value from [ObjectRequired] attribute like this:
filterContext.ActionParameters["object"] = _someObject;
I don't want to use constructions like ViewData. Where's the right way?
I'm not 100% on what I'm about to say but I'll say it as if I am...
You can't pass an object through to a get. You could pass back parameters and use a custom route handler to build the object for you.
You could also hold the object in data and use Entity Framework to get the model.
You could also use TempData, but that's similar to ViewData.
You could also hold it in their Session.
I created a static scaffolding for my domain class and got a controller for that domain class. The create action of the controller looks like:
def create() {
[userInstance: new User(params)]
}
I wonder why the line:
[userInstance: new User(params)]
has been added. Obviously when the create action is invoked, there wont be any params - so why would this line have been added?
Obviously when the create action is invoked, there wont be any params
Not necessarily - imagine a situation where you want to pre-populate a couple fields in the form of the create view. You could use:
/app/user/create?username=myusername
Which would result in the view's userInstance having a populated username field for display in the form.
This is fundamentally by Spring, the action is called 'binding' and is the action of tie form elements from one jsp(gsp in this case) to the properties of an object and viceversa.
To tie an object to a form, well, you should create it first, how can ypu tie a null object? it's not possible, that is why the new ClassObject(...)
After that in Groovy we have POGO's, and one feature of POGO's is the ability of initialize them with a map, for example:
new User(name:'John',lastname:'Zuñiga')
But in this case there's a lil' of groovy magic with that 'params' object. That comes from Groovy Servlets or Groovlets. How can you obtain a param with Java incoming from a form? Well, with request.getParam("param_name"), but in this case with Groovy you receive a params object, this params object is a map, a Groovy map...Uhm, one second...
If POGO's in Groovy is able to receive a Map as constructor, and the params object is a Map...maybe...oh coolness I can put that map in the constructor of my object, and after Spring do the binding to the form with this new Object, so this object is travelling in actions from this controller so it comes with the properties populated.
I hope this explanation be clear, if you have questions, I'm here...
Regards
There could be params, although in general there wouldn't be.
It allows pre-loading of values, which may be helpful sometimes, including re-displaying the create form.
Due to some refection-fu I want to use the MVC ModelBinders to bind the request to an object with a name and Type that is only known at runtime.
e.g.
public ActionResult BindSomething()
{
Type type = typeof(Some.Type);
string parameterName = "someParameter"; //this corresponds to a particular form input name
var binder = Binders.GetBinder(desiredType, true);
var x = binder.BindModel(this.ControllerContext, ???) //??? should be a ModelBindingContext. Where can I get this from
return View(x);
}
I think I need to get hold of the ModelBindingContext, or create a new, valid one, but how do I do this?
edit: apologies if I wasn't clear enough - I already know about TryUpdateModel, but, as far as I understand it that binds ALL the posted values to properties of the model object you pass in. I just want to get the corresponding object for a single posted parameter.
You can use TryUpdateModel as rouen suggested, you could also implement a custom model binder that can bind the correct type. This approach has the advantage of letting you deal with Interfaces or Abstract models and keeps your binding code out of your actions. It's a little neater but I would only really recommend it if it's going to be reused in other parts of your code.
Use controller method TryUpdateModel
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.tryupdatemodel.aspx
It will choose appropriate binder according argument type and perform model binding for you.
I am writing my first ASP.Net webpage and using MVC.
I have a string that I am building in a partial view with a grid control (DevExpress MVCxGridView). In my partial view I am using a HTML.Hidden helper as shown below.
' Create a hidden variable to pass back a comma-delimited string
Response.Write(Html.Hidden( "exclusionList", Model.ExclusionList))
The value of of this hidden element is assigned in client side javaScript:
exclusionListElement = document.getElementById("exclusionList");
// ...
exclusionString = getExclusionString();
exclusionListElement.value = exclusionString;
This seems to work without problem.
In my controller action method:
<AcceptVerbs( HttpVerbs.Post )> _
Public Function MyPartialCallback(updatedItemList As myModel) As ActionResult
Dim myData As myModel = GetMyModel()
Return PartialView( "MyPartial", myModel.myList )
End Function
The updatedItemList parameter is always nothing and exclusion list exists no where in the Request.Forms.
My questions are:
What is the correct way to use Html.Hidden so that I can access data in a MVC Controller Action method.
Is adding "cargo" variables to Request.Form the best and only way to send data back to a server side MVC Controller Action method? It just seems like twine and duct-tape approach. Is there a more structured approach?
If you need to get the exclusionList variable back, you just need to add a property to your view model that matches that name exactly. Make sure it is of the correct type (string it looks like in this case) and then it should auto populate that property in the view model for you.
And yes, there is no need for the Response.Write call. Instead just use the Html.HiddenFor(...) helper in your view.
Look at the generated HTML. Note down the name attribute of the hidden field. Use this name as action parameter name:
Public Function MyPartialCallback(exclusionList As string)
I have an actionlink that on click im passing a List of objects to a controller action.
Example:
View:
Html.ActionLink("TestLink", "TestMethod", "Test", Model.SampleList, null)
TestController:
public ActionResult TestMethod(List<SampleList> sampleList)
{
return View(sampleList);
}
When I do this I get a null sampleList. I can pass a single complex object fine just not a collection of it. Do I need the correct routing for this? The reason I'm doing this is instead of passing an id and do a look up in the controller action, I just pass in the data.
It is possible when you perform a form post, have a look at this blog post for more information. You'll probably not be able to use one of the HtmlHelper methods though, the post states:
Currently, we don’t have any helpers
for generating the form, so this is a
very manual process.
Nothing prevents you from writing your own helper though.