i´m using the create method of the controller to generate the paid order for a provider.
when load the method in the controller, i select by default, the first provider and with this i get the documents to pay, and other data of this provider i need to show in the view.
then render the view, whit the default provider data.
in the view i added a dropdown list of provider, where the user can change to shows the same data but for an other provider.
so:
1. What's the way to do that when the user change the provider in the combo, it call the same method create (not the [HTTPPOST]) and pass the provider id as parameter.
i´m using the create method of the controller to generate the paid order for a provider.
when load the method in the controller, i select by default, the first provider and with this i get the documents to pay, and other data of this provider i need to show in the view.
then render the view, whit the default provider data.
in the view i added a dropdown list of provider, where the user can change to shows the same data but for an other provider.
so:
1. What's the way to do that when the user change the provider in the combo, it call the same method create (not the [HTTPPOST]) and pass the provider id as parameter.
thanks in advance
You need to use a DataBound View to pass data back to the controller or pass it back in the ViewBag object.
To pass the id as a parameter to a controller method, you need to encode something like this:
Read the rest of my post, as it appears that you stoped reading at the example I used.
The only two opeions for returning a value from a View are Model or ViewBag. Stop wasting your time looking for anything else.
I will tell you that ViewBag doesn't always work as expected, so if you want the answer that will work the first time than create a Model class for your Views.
Eric, i really appreciete your help, but i don´t understand how to make a databound that handle the combobox change event and return the value to the controller.
If you could give me an example that clarify this, i´ll thanks.
the links opcion is not good for this, because i don´t know the providerID until the user select one in the dropdown.
When i press the submit button in my view, it go to the httppost method of the controller to the PayOrden object and not the provider object.
If i create a model for provider, inside a view that have the model for pay order, how do i handle what method it should call?
views do not call methods. they render html that is sent to the browser. without javascript, the only way for the rendered html to perform another request (invoke controller action) is via a link, or form submit. you pass data to the action via a query string,
or a form post (both of which use the same format for the name/value pairs).
the MVC binder looks at the posted names and loook for parameter or if the paramter is a model, then properties that match.
using javascript you can attach to the onchange event of the dropdwn and perform a form submit or ajax call.
It's really simple, say for example that your combobox is called Foo, all you need in your Model class is a Property called Foo. The MVC binder will take care of the magic of getting your combobox selected value into your Model class.
it`s true, but i need that all the atributes in the pay order change, when the user select a diferent provider. then i will use the selectd to take values.
sepilrat
Member
119 Points
87 Posts
calling a specific method from the view, and send one parameter
Feb 14, 2012 05:17 PM|LINK
HI.
i´m using the create method of the controller to generate the paid order for a provider.
when load the method in the controller, i select by default, the first provider and with this i get the documents to pay, and other data of this provider i need to show in the view.
then render the view, whit the default provider data.
in the view i added a dropdown list of provider, where the user can change to shows the same data but for an other provider.
so:
1. What's the way to do that when the user change the provider in the combo, it call the same method create (not the [HTTPPOST]) and pass the provider id as parameter.
thanks in advance
eric2820
Contributor
2777 Points
1161 Posts
Re: calling a specific method from the view, and send one parameter
Feb 14, 2012 05:43 PM|LINK
You need to use a DataBound View to pass data back to the controller or pass it back in the ViewBag object.
To pass the id as a parameter to a controller method, you need to encode something like this:
@Html.ActionLink( "link text", "methodName", "controllerName", new { parameterName = @Model.value }, null)That works for links, I'm not sure of how to make it work for a comboBox control. So that takes us back to my first suggestion.
Create a model class for your View. In the [HttpGet] method create a new instance of your model class to pass to the View.
In the [HttpPost] method, your parameter is the model class populated with the users data.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
sepilrat
Member
119 Points
87 Posts
Re: calling a specific method from the view, and send one parameter
Feb 14, 2012 06:10 PM|LINK
Thanks for reply.
i need what you propose as link, in the combobox.
my methods:
here is where i need to pass parameter:
public ActionResult Create(int? id)
{
if id is null, set the first provider as default, else i search the provider by id.
}
in the next method i save the pay order in the database, so i can´t use this to drive the dropdow list change.
[HttpPost]
public ActionResult Create(OrdenPago ordenpago)
{
if (ModelState.IsValid)
{
db.OrdenPago.Add(ordenpago);
db.SaveChanges();
return RedirectToAction("Index");
}
}
eric2820
Contributor
2777 Points
1161 Posts
Re: calling a specific method from the view, and send one parameter
Feb 14, 2012 06:28 PM|LINK
Read the rest of my post, as it appears that you stoped reading at the example I used.
The only two opeions for returning a value from a View are Model or ViewBag. Stop wasting your time looking for anything else.
I will tell you that ViewBag doesn't always work as expected, so if you want the answer that will work the first time than create a Model class for your Views.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
sepilrat
Member
119 Points
87 Posts
Re: calling a specific method from the view, and send one parameter
Feb 16, 2012 03:19 PM|LINK
Eric, i really appreciete your help, but i don´t understand how to make a databound that handle the combobox change event and return the value to the controller.
If you could give me an example that clarify this, i´ll thanks.
the links opcion is not good for this, because i don´t know the providerID until the user select one in the dropdown.
When i press the submit button in my view, it go to the httppost method of the controller to the PayOrden object and not the provider object.
If i create a model for provider, inside a view that have the model for pay order, how do i handle what method it should call?
Thanks in advance
bruce (sqlwo...
All-Star
36836 Points
5443 Posts
Re: calling a specific method from the view, and send one parameter
Feb 16, 2012 03:27 PM|LINK
views do not call methods. they render html that is sent to the browser. without javascript, the only way for the rendered html to perform another request (invoke controller action) is via a link, or form submit. you pass data to the action via a query string, or a form post (both of which use the same format for the name/value pairs).
the MVC binder looks at the posted names and loook for parameter or if the paramter is a model, then properties that match.
using javascript you can attach to the onchange event of the dropdwn and perform a form submit or ajax call.
eric2820
Contributor
2777 Points
1161 Posts
Re: calling a specific method from the view, and send one parameter
Feb 16, 2012 04:14 PM|LINK
It's really simple, say for example that your combobox is called Foo, all you need in your Model class is a Property called Foo. The MVC binder will take care of the magic of getting your combobox selected value into your Model class.
http://www.my-msi.net/Admin
blog
If a post helps you, please mark it as Ansered, thank-you.
sepilrat
Member
119 Points
87 Posts
Re: calling a specific method from the view, and send one parameter
Feb 19, 2012 06:28 PM|LINK
it`s true, but i need that all the atributes in the pay order change, when the user select a diferent provider. then i will use the selectd to take values.
sepilrat
Member
119 Points
87 Posts
Re: calling a specific method from the view, and send one parameter
Feb 19, 2012 06:31 PM|LINK
I think it`s the way. How do i pass the value of the combobox to a method by using javascript?
i need to point to that method in the controller:
public ActionResult Create(int? id) { if id is null, set the first provider as default, else i search the provider by id. }Thanks in advance