[英]How do I get the correct radio button selected using MVC RadioButtonFor with float values?
我有一个编辑器模板持有Duration
,它有四个简单的单选按钮。
我在我的类Test
上使用此模板作为属性。 我无法将此属性绑定到单选按钮列表并在加载时选择正确的单选按钮。 选择值并提交将返回正确的值。 我已经尝试将单选按钮的值更改为各种不同的值,但无济于事。
我应该将什么值传递给RadioButtonFor
以使其工作? 理想情况下,这个编辑器模板采用可空的浮点数仍然有效。
Duration.cshtml
@model float
<h3>Model Value: @Model</h3>
@Html.RadioButtonFor(model => model, 0.25F, new { id = "btnQuarterDay" }) @(.25F)
@Html.RadioButtonFor(model => model, 0.5F, new { id = "btnHalfDay" }) @(.5F)
@Html.RadioButtonFor(model => model, 0.75F, new { id = "btnThreeQuarterDay" }) @(.75F)
@Html.RadioButtonFor(model => model, 1.0F, new { id = "btnOneDay" }) @(1.0F)
test.cs中
public class Test
{
[UIHint("Duration")]
[Display(Name = "Days")]
public float Duration { get; set; }
}
HomeController的
public class HomeController : Controller
{
public ActionResult Index(float? duration = null)
{
var model = new Test
{
Duration = duration.GetValueOrDefault(1F)
};
return View(model);
}
[HttpPost]
public ActionResult Index(Test model)
{
ViewBag.Success = true;
ViewBag.ValueSelected = model.Duration;
return View(model);
}
}
Index.cshtml
@model RadioButtonForTest.Models.Test
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<div class="row">
<div class="col-md-4">
@Html.EditorFor(model => model.Duration)
@Html.ValidationMessageFor(model => model.Duration)
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
if (ViewBag.Success ?? false)
{
<span>Value Selected:</span> @ViewBag.ValueSelected
}
}
更新:Plot Thickener
我将索引操作连接到持续时间,以便我可以通过查询字符串传递它。 如果未通过查询字符串传递值。 它将选择下面的单选按钮:
@Html.RadioButton("", Model, Model.Equals(1.0F), new { id = "radbtnOneDay4" })
如果我导航到查询字符串/Home/Index?duration=.5
则选择NOTHING,但在调试Duration.cshtml
Model.Equals(.50F)
报告为TRUE
。 所以我希望这个单选按钮被选中:
@Html.RadioButton("", Model, Model.Equals(.50F), new { id = "radbtnHalfDay4" })
这不是疯了吗? 为什么不检查这个单选按钮??? 这是一个错误吗? 是否有我可以添加的自定义模型粘合剂来处理浮动?
更新2
查询字符串参数duration
与模型名称匹配,查询字符串覆盖模型值。
当我将参数更改为dur
它工作正常。 使用以下URL,它将使用ViewModel中的值。 /Home/Index?dur=.5
我知道这背后有一个坚实的原因......但这令人恼火。
RadioButtonFor帮助器在模板中不起作用(编辑:...在像你这样的情况下使用它来绑定到简单的成员值,因为模型=>模型表达式将变为空名称)。 您需要将模板代码更改为
@Html.RadioButton("", 0.25F, Model == 0.25F, new { id = "btnQuarterDay" }) @(.25F)
@Html.RadioButton("", 0.5F, Model == 0.5F, new { id = "btnHalfDay" }) @(.5F)
@Html.RadioButton("", 0.75F, Model == 0.75F, new { id = "btnThreeQuarterDay" }) @(.75F)
@Html.RadioButton("", 1.0F, Model == 1.0F, new { id = "btnOneDay" }) @(1.0F)
编辑#2:另一种选择是使用更复杂的模型:
public class DurationContainer {
public float Duration { get; set; }
}
public class Test2 {
[UIHint("Duration2")]
public DurationContainer Container { get; set; }
}
Index.cshtml:
@Html.EditorFor(model => model.Container)
然后作为Duration2.cshtml模板:
@Html.RadioButtonFor(model => model.Duration, 0.25F, new { id = "btnQuarterDay" }) @(.25F)
@Html.RadioButtonFor(model => model.Duration, 0.5F, new { id = "btnHalfDay" }) @(.5F)
@Html.RadioButtonFor(model => model.Duration, 0.75F, new { id = "btnThreeQuarterDay" }) @(.75F)
@Html.RadioButtonFor(model => model.Duration, 1.0F, new { id = "btnOneDay" }) @(1.0F)
您的编辑器模板不起作用,因为第一个参数应该是模型属性的成员表达式而不是模型本身,因此model => model
是错误的。 您可以使用Html.RadioButton
而不是`Html.RadioButtonFor轻松修复它。 请注意,我们只是传递一个空字符串,而不是指定属性名称。 原因是EditorTemplates生成通过在parrent模型中添加前缀来生成正确的属性名称。 所以你的模板应该是这样的:
@model float
<h3>Model Value: @Model</h3>
@Html.RadioButton("", 0.25F, new { id = "btnQuarterDay" }) @(.25F)
@Html.RadioButton("", 0.5F, new { id = "btnHalfDay" }) @(.5F)
@Html.RadioButton("", 0.75F, new { id = "btnThreeQuarterDay" }) @(.75F)
@Html.RadioButton("", 1.0F, new { id = "btnOneDay" }) @(1.0F)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.