Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
hi to all,
Here i want to set radio button in mvc.

CONDITION:-
Here i have three radio button for transport fields
transport:
-car
-bus
-train


Here also three radio button for total number of passenger:-
passenger:
-4
-40
-200


If i click car in transport then in passenger fields 4 is automatically selected and others is disable, click bus 40 is selected automatically other radio button is disabled.

Can anyone tell how to write code in controller and partial-view can anyone explain me.

[Added this from comment below]

View:
HTML
<div class="col-md-3 text-left">
 
@Html.Label("Car", new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.TextBoxFor(model => model.Car, new { @class = "form-control control-text" })
@Html.ValidationMessageFor(model => model.Car)
</div>
 
</div>
 
<div class="form-group">
<div class="col-md-3 text-left">
 
@Html.Label("Passenger", new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.TextBoxFor(model => model.Passenger, new { @class = "form-control control-text" })
@Html.ValidationMessageFor(model => model.Passenger)
</div>

Modal:-
C#
[Index(IsUnique = true)]
[Required(ErrorMessage = "Car is required.")] 
[StringLength(30)]
public string Car{ get; set; }
 
[Index(IsUnique = true)]
[Required(ErrorMessage = "Passengeris required.")] 
[StringLength(30)]
public int Passenger{ get; set; }
Posted
Updated 20-Aug-14 4:19am
v3

1 solution

Well, what model do you pass to your partial view?
Reading your question it sounds like you need a model which tells you what is selected then you can,
in the partial view, set the initial states of the check boxes.
In turn, depending on the rest of your view/page/site put in jQuery so that on the client side if they change the "transport" the appropriate passenger changes.

---------Below is based on code provided-------------
This is sample code it may need tweaking but should give you sufficient to get started. It is also assumed that you are surrounding all of your view in a "Form" which submits to an action expecting your model. It also assumes you are using bootstrap classes (as it appears you might be so the styling might be slightly out).

HTML
@Html.Label("Vehicle", new { @class = "control-label" })
</div>
<div class="col-md-3">
<label class="radio-inline">
  <input type="radio" name="Car" id="inlineCar" value="Car" @(Model.Car.Equals("Car") ? "Checked" : String.Empty)> Car
</label>
<label class="radio-inline">
  <input type="radio" name="Car" id="inlineBus" value="Bus"  @(Model.Car.Equals("Bus") ? "Checked" : String.Empty)> Bus
</label>
<label class="radio-inline">
  <input type="radio" name="Car" id="inlineTrain" value="Train"  @(Model.Car.Equals("Train") ? "Checked" : String.Empty)> Train
</label>
@Html.ValidationMessageFor(model => model.Car)
</div>


Do the same for your number of seats, but also have a check to see if you need to disable any of the radio buttons.

Then you need to write some jquery code that works when the vehicle is changed to update the selected number of seats.

Hope this gives you enough to accomplish your objective.
 
Share this answer
 
v2
Comments
JOTHI KUMAR Member 10918227 20-Aug-14 9:47am    
thanks for reply i'm new for mvc5 .so could you explain me detailed sir
Pheonyx 20-Aug-14 9:53am    
What have you tried? What have you looked up? I'm not going to do your work for you.
You haven't provided sufficient detail for me to provide any code example that might be useful to you.

1) Design your data model
2) Design your action
3) Create your partial view based on your model
4) In the action populate your data model and pass it to your partial view, return said partial view.

If you've not done any MVC before I suggest you read the tutorials that are available here: http://www.asp.net/mvc/tutorials as they explain all the basics.
JOTHI KUMAR Member 10918227 20-Aug-14 10:03am    
here i designed entities,dbset,controller and view using textbox. but now i want to set radiobutton instead of textbox and populate values.
Pheonyx 20-Aug-14 10:06am    
Use the "Improve Question" button and update your question with code snippets. Show us your "Model" and your "View" then maybe we can look at guiding you but you are not providing any additional information in any of your responses.
JOTHI KUMAR Member 10918227 20-Aug-14 10:13am    
view:
<div class="col-md-3 text-left">

@Html.Label("Car", new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.TextBoxFor(model => model.Car, new { @class = "form-control control-text" })
@Html.ValidationMessageFor(model => model.Car)
</div>

</div>

<div class="form-group">
<div class="col-md-3 text-left">

@Html.Label("Passenger", new { @class = "control-label" })
</div>
<div class="col-md-3">
@Html.TextBoxFor(model => model.Passenger, new { @class = "form-control control-text" })
@Html.ValidationMessageFor(model => model.Passenger)
</div>


Modal:-

[Index(IsUnique = true)]
[Required(ErrorMessage = "Car is required.")]
[StringLength(30)]
public string Car{ get; set; }

[Index(IsUnique = true)]
[Required(ErrorMessage = "Passengeris required.")]
[StringLength(30)]
public int Passenger{ get; set; }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900