Samavi
Samavi

Reputation: 69

How to get the value of selected checkbox in Asp.net MVC?

I want to print the value /values of checkbox selected by the user. For that purpose I have taken a String[] checkResp.

For Example: If checkResp[0] then select value at index 0.

View

<input type="checkbox" id="vehicle1" name="checkResp" value="Bike">
<label for="vehicle1"> I have a bike</label><br>
<input type="checkbox" id="vehicle2" name="checkResp" value="Car">
<label for="vehicle2"> I have a car</label><br>
<input type="checkbox" id="vehicle3" name="checkResp" value="Boat">
<label for="vehicle3"> I have a boat</label><br><br>

Controller

public ViewResult Form(string receiveremail, string subject, string message,string name, int qty, string[] checkResp) 
{
        Body = "Dear " + name +" Your Order have been successfully Placed. Number of products placed are " + qty + "from " + qty + " Store" + checkResp;
}

ScreenShot is attached herewith:

It displays the values like this i want value at index 0

Please if any one can help

Upvotes: 1

Views: 123

Answers (1)

Jerdine Sabio
Jerdine Sabio

Reputation: 6130

If you want to add both products use String.Join(delimiter,array);

Body = "Dear " + name +" Your Order have been successfully Placed. Number of products placed are " + qty + "from " + qty + " Store" + String.Join(",",checkResp);

If you want a single item use array[index];

Body = "Dear " + name +" Your Order have been successfully Placed. Number of products placed are " + qty + "from " + qty + " Store" + checkResp[0];

Upvotes: 1

Related Questions