Binding 2 Dropdownlist in ASP.NET mvc c# - 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+ "");

Related

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.

Pass drop down list's selected value as a query string in an ActionLink in an MVC view

I have a simple drop down list in my view:
#Html.DropDownList("ddlAppIds", new SelectList(ViewBag.AppIds))
And right below it, I have an ActionLink. I want to send the selected value of the above drop down list as a query string parameter inside this ActionLink:
#Html.ActionLink("Next", "Index", new { id = selectedAppId })
selectedAppId is just for demonstration purposes of what I want to hopefully accomplish. How can I pass the drop down list's selected value as a query string parameter in my view?
Unless you happen to already have a selected value at page load, MVC has no way of knowing the user selects without a postback. Once the page has been sent to the client, MVC is done until the next request: your Razor view has already been rendered and no changes will be made to anything in the DOM without you manually doing it via JavaScript.
If you want to change the link based on the selected value, then you will need to write JavaScript to make that happen. Since you're using MVC, I'm going to assume jQuery support, because it's included by default:
$(document).ready(function () {
$('#ddlAppIds').on('change', function () {
var selectedValue = $(this).val();
$('#IdForYourLink').prop('href', '/url/to/next/' + selectedValue);
});
});
You'll need to figure out on your own the best way to replace the selected value. You might prefer to do some sort of regex on the current link, only include a partial link as the href to begin with, etc.

POST Row Id to a new page for editing

Please help me out here I have about 1 day experience of C# ASP.NET web dev and I am stuck on something really simple. I am much more experienced in PHP and jQuery.
What I need to do:
Create a GridView and display data from MSSQL DB [done]
Add buttons to each row to enable direct editing of values in two columns [done]
Each row represents a list of equipment that the user can edit/save, and I need to add an extra "Edit Details" button to each row [done]
Upon clicking the "Edit Details" button, I need to pass the id of that list to a new page where user can edit and save the list
I have added a HyperLinkField for the row id to be passed on, in Visual Studio I have tried to config it so that the value can be read on the receiving page with Request.Params["xxxx"] but it is not working.
In PHP I would just create a POST <form> with hidden <input> values and obtain the value by $_POST["xxxx"]
However it seems that all this fancy ASP.NET and Visual Studio DataSource pointer/reference and config things are making things more complicated than I wished.
I greatly appreciate any help given, or any resources that will be helpful for me to transfer my skills in PHP over to ASP.NET with C#.
If you are passing it through query string use
string value = Request.QueryString["id"].ToString();;
If you are passing it through post use
string value = Request.Forms["id"].ToString();

formmethod.post vs formmethod.get in mvc3?

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?

C# Update fields belonging to the Id in the query string

I m a newbie to C# and Ive got this doubt please help.
I have these 2 webpages lets call them pageA and pageB .PageA has got a table(used DataGrid) displaying all the fields of a table in sql server ,it has got an additional edit column which displays a link to PageB, the url for pageB has got a query string which contains the ID of the product that is to be updated .In PageB i have got an update form. The form fields of which is already filled (i.e im retrieving data using the ID from the query string using "select").This pageB has got a button which is supposed to update the fields that were edited in this page and store them back to the server .
In pageB :the Page_Load() is where i save the ID to a variable (VarID) and i call the method fill_data_fields() which does the "select" and fills the fields .I ve also got the update_click() which is called when i click the button on pageB it is in this method I ve written the code to update the fields in database .
The PROBLEM is ,that the method update_click() is never called when i click the button.
Any chance that you're adding the button programmatically rather than defining it in the asmx markup? ASP.Net is horrible with dealing with that issue. Also make sure you've wired your update_Click() button correctly in the asmx markup as well. Remember, C# is case sensitive!

Categories