使用@html.RadioButtonFor MVC4 ASP.Net绑定4个单选按钮

时间:2017-11-16 08:00:04

标签: asp.net-mvc-4 view enums radiobuttonfor

这是我的ENUM:

<label><b>Zero Validity:</b></label>
@if (Model.ValidityType == Constant.ValidityTypes.ZeroValidity) 
{
    @Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" })             
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "1", new { @id = "validity1", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" })             
}
<label><b>Life Time Validity: </b></label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.LifeTimeValidity) 
{
    @Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "true" , onClick = "ValidityEnableDisable(this);" }) 
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "2", new { @id = "validity2", style = "width:50px", @checked = "false" , onClick = "ValidityEnableDisable(this);" }) 
}
<label><b>'N' Months:</label>
@if (Model.ValidityType == OneC.BGV.BE.Constant.ValidityTypes.NMonths) 
{ 
    @Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "true", onClick = "Validity3EnableDisable(this);" ,  autocomplete="off" }) 
    @Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px" })
}
else
{
    @Html.RadioButtonFor(x => x.ValidityType, "3", new { @id = "validity3", style = "width:50px", @checked = "false" , onClick = "Validity3EnableDisable(this);" ,  autocomplete="off"})
    @Html.TextBoxFor(x => x.ValidityValue, new { id = "NmonthTextbox", style = "width:50px", disabled = "true" })
}

这里我设置了Model属性:

model.Validitytype

这是我的观点:

ZeroValidity

现在,当我加载此视图时,我ifN months,与数据库中的相同,并且满足第一个@checked条件。但是在完整视图加载时,将检查case class InverterResponse(body: InverterBody) extends SolarResponse case class InverterBody(data: InverterData) case class InverterData(dayEnergy: DayEnergy) case class DayEnergy(unit: String, values: Values) case class Values(value: Int) case class MeterResponse(body: MeterBody) extends SolarResponse case class MeterBody(data: MeterData) case class MeterData(powerRealSum: BigDecimal, powerRealPhase1: BigDecimal, powerRealPhase2: BigDecimal, powerRealPhase3: BigDecimal) class SolarWebConnector extends JsonSupport { implicit val system = ActorSystem() implicit val materializer = ActorMaterializer() val httpClient = Http().outgoingConnection(host = "192.168.178.22", port = 80) def getInverterRealtimeData(): InverterData = { val inverterRealtimeURLPath = s"""/solar_api/v1/GetInverterRealtimeData.cgi?scope=System""" val flowGet: Future[InverterResponse] = sendRequest[InverterResponse](inverterRealtimeURLPath) val start = System.currentTimeMillis() val result = Await.result(flowGet, 5 seconds) val end = System.currentTimeMillis() println(s"Result in ${end - start} millis: $result") result.body.data } private def sendRequest[T](inverterRealtimeURLPath: String) : Future[T] = { val flowGet: Future[T] = Source.single( HttpRequest( method = HttpMethods.GET, uri = Uri(inverterRealtimeURLPath)) ) .via(httpClient) .mapAsync(1)(response => Unmarshal(response.entity).to[T]) .runWith(Sink.head) flowGet } def getMeterRealtimeData(): String = { "test" } } 单选按钮。

为什么我无法显示正确的单选按钮? 是控件的Error:(54, 63) Play 2 Compiler: /Users/tbecker/workspaces/home-integrator/app/services/SolarWebConnector.scala:54:63: could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.Unmarshaller[akka.http.scaladsl.model.ResponseEntity,T] .mapAsync(1)(response => Unmarshal(response.entity).to[T]) ^ 属性不起作用吗?

1 个答案:

答案 0 :(得分:1)

checked="true"checked="false"checked="anythingAtAll"都意味着相同的事情 - 将会检查单选按钮。 checked属性是boolean属性,这意味着它的存在决定了它是否被检查。在你的情况下,总是检查最后一个,因为这是最后一个被渲染的(该属性从之前的属性中删除,因为只允许检查一个属性)。

您永远不应该设置checked属性,因为RadioButtonFor()方法会根据您绑定的模型的值来执行此操作(即模型绑定的工作方式)。删除所有if/else块并将代码更改为

<label>
    @Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.ZeroValidity, new { id = "", @class = "validity" })
    <span>Zero Validity</span>
</label>
<label>
    @Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.LifeTimeValidity, new { id = "", @class = "validity" })
    <span>Life Time Validity</span>
</label>
// labels below omitted for brevity
@Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.NMonths, new { id = "", @class = "validity" })
@Html.RadioButtonFor(x => x.ValidityType, ValidityTypes.RehireValidity, new { id = "", @class = "validity" })

然后使用css来设置元素的样式而不是内联样式,例如

.validity {
    width: 50px;
}

并使用Unobtrusive JavaScript来处理您的活动

$('.validity').click(function() {
    ....
});