如何为各个字段设置radiobutton并在mvc5中给出一些条件 [英] how to set radiobutton for respective fields and give some condition in mvc5

查看:232
本文介绍了如何为各个字段设置radiobutton并在mvc5中给出一些条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

这里我想在mvc中设置单选按钮。



条件: -

这里我有三个运输领域的单选按钮

运输:

-car

-bus

-train




这里还有三个乘客总数的单选按钮: -

乘客:

-4

-40

-200




< b>如果我在运输中点击汽车,那么在乘客区域4会自动选择而其他人被禁用,点击公共汽车40会自动被选中,其他单选按钮被禁用。



任何人都可以告诉我如何在控制器和部分视图中编写代码可以任何人解释我。



[从下面的评论添加此内容]



查看:

 <   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 { @cl ass =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 >



模态: -

<前lang =c#> [索引(IsUnique = true )]
[必需(ErrorMessage = 需要汽车。)]
[StringLength( 30 )]
public string Car {< span class =code-keyword> get ; set ; }

[索引(IsUnique = true )]
[必需(ErrorMessage = 需要Passengeris。)]
[StringLength( 30 )]
public int Passenger { get ; set ; }

解决方案

那么,你传递给局部视图的模型是什么?

读它的问题听起来就像你需要一个模型告诉你选择了什么,那么你可以在局部视图中设置
,设置复选框的初始状态。

反过来,取决于您的视图/页面/网站的其余部分放在jQuery中,以便在客户端如果他们更改运输适当的乘客更改。



---- -----以下是基于提供的代码-------------

这是可能需要调整的示例代码,但应该足以让您开始使用。还假设您在表单中围绕所有视图,该表单提交了期望您的模型的操作。它还假设您正在使用引导类(因为它看起来可能是因为样式可能稍微偏离)。



 @ Html .Label(Vehicle,new {@class =control-label})
< / div >
< div class = col-md-3 >
< label class = radio-inline >
< 输入 type = radio name = Car id = inlineCar value = Car @(Model.Car.Equals(Car) 已检查 String.Empty) > Car
< / label >
< label class = radio-inline >
< 输入 type = radio name = Car < span class =code-attribute> id = inlineBus value = 总线 @(Model.Car.Equals(Bus) 已检查 String.Empty) > 总线
< / label >
< label class = radio-inline >
< 输入 type = radio name = Car id = inlineTrain value = < span class =code-keyword> Train
@(Model.Car.Equals(Train) 已检查 String.Empty) > 训练
< / label >
@ Html.ValidationMessageFor(model => model.Car)
< / div >





对你的座位数量做同样的事情,但也要检查你是否需要禁用任何一个单选按钮。



然后你需要编写一些jquery代码,当车辆改变时更新选定的座位数。



希望这足以让你完成目标。


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:

<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; }

解决方案

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.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.


这篇关于如何为各个字段设置radiobutton并在mvc5中给出一些条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆