formmethod.post vs formmethod.get in mvc3? - c#

i'm working on a cascading dropdownlist in mvc3, when i used formmethod.post im able to select a value in dropdownlsi1 and based on the that selection im able to get values in dropdownlist2 and after selecting an item in dropdownlist2 , im getting a button which on clicking will give the user a message u have selected X and Y.....this is fine and i have no issue with it
but when i used formmethod.get, im able to get the 1st dropdownlist and based on the selection in the 1st dropdownlist im able to get 2nd dropdownlist nd items in the 2nd dropdownlist, after the selection in the 2nd dropdownlist , i got a button , but after clicking the button, again im seeing the same page when i get when i load the page i.e only the 1t dropdownlist..
In what scenario we should use formmethod.post and formmethod.get?

You use formmethod.get when you make http get request and formmethod.post when you make http post request it is methods of parameters. It seems to me your solution is to pass previous selected values of dropdown lists into the view and initialize helpers with selected values to render them properly.
It could be done with both request types.

You have two actions:
[HttpGet]
[HttpPost]
In first action you load page, if you use Post method, then you send Post data to second action. If you use Get method you just reload form action, it also send data, but they are included in url, like this site.com?param=1&test=2.
GET or POST method? What's the difference between them?

Related

Binding 2 Dropdownlist in ASP.NET mvc c#

This is my first dropdown list in my view.:
And this is my controller:
My problem is that whenever I click the available dropdown in my first dropdown list it should populate the data available on the one that I have clicked on the second dropdown list. But It has an error it will not proceed to the next line which the JavaSerializer. How can I fix my code like it will work. I don't know where is the problem is, whether in the ajax? or in my controller?
Please change the select as jQuery("#availability option:selected").val();
- Change the ajax type as GET method in client side
- JavascriptSerializer not required
- remove [httppost] tag from code behind
- check where any error is there in browser console.
and change $('#availability').push("" + this.name+ "");

when select value from dropdwonlist i want to show it in for example div

I have a dropdownlist inside a form and a div. I want that, when i select a value from the dropdownlist, it shows in the div.
I've succeeded in this, but the problem is that the following selections ,after first one, are deleting previous results from the div.
public ActionResult SelectStudWhoWantReport(int StudentId = 0)
{
IEnumerable<SelectListItem> items = db.Students.Select(c => new SelectListItem
{
Value = SqlFunctions.StringConvert((double)c.StudentID).Trim(),
Text = c.StudentName
});
ViewBag.StudentId = items;
Student WhomakeReport = db.Students.Find(StudentId);
ViewBag.Name = WhomakeReport.StudentName;
return View();
}
viewbag is the value that apear in div, When I select that how can I append the value of the second select with the first select to show the two in the div.
I think you are using a bad approach to solve your problem.
You are getting the result of the selection on your controller class, through a form submission I supose, and later send it to your View.
The problem is that when you call the controller your page is reloaded, thus you loose the previous info. There are different ways in which you'd be able to store previous results and build them on the View.
However, I think it's better that I explain you a more correct approach to solve this problem.
You should have a dropdownlist component on your View, which you load through your controller. I supose you already have this.
When the selection is made, instead of submitting the form, make an AJAX call to a method in the server which makes what you're currently doing in your controller (get the result depending on the selected id) and, instead of returning an ActionResult, just return the value you want.
When the AJAX callback returns, you'll have the result on your client code, so you just have to append it after previous results.
This approach not only is better from a MVC pattern point of view, it's also easier to implement and will allow your users to select elements in the dropdownlist and see the results without having to reload the whole page.

MVC DropDownList Select Populates ListBox (Both Selects)

I have an MVC view that has a model with a IEnumerable<SelectListItem> which populates a #Html.DropDownList (Form select).
I want to write code so that when a item in the drop down list is selected, it will populate another listbox (also a form select) with values from the server.
Should I use AJAX to get a seperate partial view that contains the listbox with the values on each click of the dropdownlist? Or should I generate a listbox on load and then use WebAPI to just get the data on each click?
It seems like a simple problem but I'm trying to figure out my models and views and the best approach.
Populate your dropdownlist
Create an empty list box.
Have a javascript function that fires on dropdown list change event and do ajax call to the action method that returns the json result.
Bind the json data to the list box on success.
I would rather go with an Ajax since other dropdown lists are dependent on your first dropdown.
and in your controller, just have a JsonResult, something like this:
public JSonResult getDropdownItems(your parameters)
{
//do some iteration in your context
return Json( /*list or iEnumerable*/, Json.AllowGet);
}

Binding Data to Asp.net HighCharts using Jquery Json

I have a asp.net chart which shows some attendance of month.
I have a dropdown which shows the month name. The user will select the month from the dropdown and click on a button.
On this button click, I want to call ActionResult mettod, which will fetch the data from a database of selected month and bind the data to chart(dotnet.highcharts).
I want to know if I can bind data to an asp.net chart with specific values.
Can anyone please let me know how to do this?
Your method would probably return JsonResult in that case. Once you receive that json on the client side (through ajax request e.g.) you would submit it to Highcharts series property.

MVC 4 Submit button in each row of a "grid" - how to?

How can I have a submit button in each row of a "grid", where, when that button is clicked, I can POST three pieces of data in the row to the controller?
Here's the scenario:
A screen shows my system's users in an html table "grid". One link in each row says "Associate Customer". This goes to another screen showing a list of customers that this user can be associated with. Their UserID is in the URL like AssociateCustomer/14
On the second screen, I want to display in an html grid, CustomerID, CustomerName, and a link/button that either:
Says "Associate" if they aren't associated with that customer, or
Says "Disassociate" if they are already associated with that customer
I got that much working, but I don't know the next part: when the user clicks a button on the grid, I need to pass CustomerID, the UserID, and "Associate/Disassociate" to the controller. How can I do that in an MVC way?
Something like:
# Html.ActionLink("Click Me!","Associate","SomeController",new{userId=item.UserID,customerID=item.CustomerID})
You don't have to post the form to do what you are asking (though that would be one way to do it).
Generally you won't post the form in this case.
I would create Associate and Disassociate actions in your controller that accept the parameters you require as querystrings. Then build an ActionLink for each row of your grid that builds that appropriate URL.

Categories