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

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.

Related

How to get the id of specific field in tabel asp.net c#

So i have a transaction table that contains only the transactions that haven't been confirmed yet.
it contains the transaction number (PK), the transaction status which is pending, and the confirmation (which is a button for uplouding a picture of the accomplished transaction). what i need is, if the user uplouded the picture, the tranaction status will change to accomplished according to which tranasction the user chose.
I do not know how to connect the uploud pic button with the transaction number. I tried to connect them by the id and retrieve the id but it didn't work. i did 4 choices of tags (, , and asp:Button) and tried to retrieve it with request.form("name") just to test if its retrieve something, but it returns nothing.
Do you have a single button separate from the table, or a button for each row in the table?
If there is a button in each row, then assign the transaction ID to the Button as a CommandArgument. Assuming you load the table from a database, it could look something like this:
<asp:Button ID="ConfirmTransaction" runat="server" CommandArgumant='<%#Eval("TransactionNumber")%>' OnClick="UploadPicture" />
Then you can retrive the CommandArgument of the button in the OnClick event and use it in your update statement.
But as said above, you could post a snippet of how your table and buttons look like, as without seeing it it's hard to give a proper answer.

Do I need to get rid of a postback or?

I've got a problem with my code.
I have this in my page load:
result = objdboDoc.searchFattura(Convert.ToInt64(Session["id_utente"]), id_cliente);
what does it do?
Well I have a gridview on the top of the page which shows me all the invoices a user submitted for a customer.
so I call the stored procedure "Look for invoices" made by "user", to "customer".
I have an another gridview in the middle of the page with all my customers.
When I click on "Select customer", the page is being posted back, and then I get the id of the customer I want to show my invoices for.
Since the page is posted back before executing the "Select customer" command, when I click select I still have the invoices of the old customer.
Only if I click select again I get the right customer's id
It's mind blowing because I need
result = objdboDoc.searchFattura(Convert.ToInt64(Session["id_utente"]), id_cliente);
to load the invoices of the customer selected in the previous page
first, the page is not posting back before "Select Customer" it's posting back because of the click.
After you process the select from the middle grid and choose a customer I assume that someplace you are setting Session["id_utente"] which is used by the first grid. after setting the session variable it should just be a matter of rebinding the first grid with a call to DataBind.
Page Life Cycle

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?

Dynamically adding buttons and adding their source code

Greeting,
I have already checked for my question with no luck of an answer or something similar.
So here it's my problem:
I have created a cafe cashier software for my boss that it has buttons for each product that cashier wants to add on a bill/table. If a button is clicked, for example btnOrangeJuice, on its click event, it call a function for example named: addOrder("Orange Juice");
Inside this function it does the simple follow code:
a) it connect to the database
b) returns price of product "Orange Juice" for example (select price from products where product_name = '" + product_parameter + "'")
c) take the result, and add it to the order list box with it's price and so on..
The problem here is, (lets go with the previews example) every time we have a new product, I have to manually customize the form by adding a button 52x52 pixels, name btnOrangeJuice, text name "Orange Juice", and on the click event to call my function addOrder("Orange Juice"), then upload the new version of my software to my online web hosting, and update the software from the cashier's pc. This is time consuming for me, especially when we make new deals with suppliers so i wanted to make my program to add dynamically buttons, to do all this by just having a form, type in the button properties, calling a function, and add a button by its self with its dimensions, check if its out of form space, if it is, the add the button bellow instead on the right, and of course add the button properties and its click event (that call my built-in function).
If you need any farther details regarding my software/problem please do ask.
So can you assist me on this please?
The standard approach is to assign each product a unique identifier and have one mechanism for adding an order which takes the unique identifier as the parameter. Your products already presumably have a unique identifier as they are persisted entities (a database primary key for example).
If you wish the add button to have the text of the product, then you can dynamically set this from the product name, which is again already stored in the database. There is no need to replicate this information in your UI code.

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