【问题标题】:How do I get the correct radio button selected using MVC RadioButtonFor with float values?如何使用带有浮点值的 MVC RadioButtonFor 选择正确的单选按钮?
【发布时间】:2016-09-29 20:18:24
【问题描述】:

我有一个编辑器模板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
    }
}

更新:绘图增稠剂
我将 Index 操作连接到持续时间,以便我可以通过查询字符串传递它。如果未通过查询字符串传递值。它将选择下面的单选按钮:

@Html.RadioButton("", Model, Model.Equals(1.0F), new { id = "radbtnOneDay4" })

如果我导航到查询字符串 /Home/Index?duration=.5 未选择任何内容,但在调试 Duration.cshtml Model.Equals(.50F) 时会报告 TRUE。所以我希望这个单选按钮被选中:

@Html.RadioButton("", Model, Model.Equals(.50F), new { id = "radbtnHalfDay4" })

这不是疯了吗?为什么不检查这个单选按钮???这是一个错误吗?是否可以添加自定义模型绑定器来处理浮动?

更新 2
查询字符串参数duration 匹配模型名称,查询字符串覆盖模型值。
当我将参数更改为dur 时,它工作正常。使用以下 URL,它将使用 ViewModel 中的值。 /Home/Index?dur=.5
我知道这背后有充分的理由……但这令人恼火。

【问题讨论】:

  • 不确定为什么它不能与您的 EditorTemplate 一起使用,但如果您直接在主视图中包含 @Html.RadioButtonFor(m =&gt; m.Duration, 0.25F) 等,它会起作用
  • 绘图加厚:有时 html 助手用于模型值的内容与调试器显示的内容有所不同。请参阅stackoverflow.com/questions/1775170/… 了解更多信息

标签: c# asp.net-mvc asp.net-mvc-5


【解决方案1】:

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)

Edit#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)

【讨论】:

  • 实际上RadioButtonFor 在模板中效果很好。 model =&gt; model 表达式导致的问题 - 这是无效的。
  • @AlexArt。在为此花费了数小时后,我必须恭敬地不同意。
  • @Pitchmatt 感谢您的回复。请参阅上面的更新。我正在使用Model.Equals,即使调试器说应该选择它,我也没有运气。我什至无法获得正确的发布价值。
  • @Airn5475 我刚刚再次测试了第一个版本,它适用于我,甚至适用于帖子。请注意@Html.RadioButton(...part 中的固定错字,因此如果您在几个小时前复制了第一个模板帖子,您可能需要再次复制它
  • @Pitchmatt 是的,我在调试时发现了同样的错字,并在上面的第一个更新中发布了更改。我也考虑过您的替代方法,但似乎增加了额外的复杂性。感谢您对此的帮助!
【解决方案2】:

您的编辑器模板不起作用,因为第一个参数应该是模型属性的成员表达式,而不是模型本身,所以 model =&gt; model 是错误的。您可以使用 Html.RadioButton 而不是 `Html.RadioButtonFor 轻松修复它。请注意,我们只是传递一个空字符串,而不是指定属性名称。其原因是 EditorTemplates 生成通过在父模型中附加前缀来生成正确的属性名称。所以你的模板应该是这样的:

@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)

【讨论】:

  • 感谢您的回复。不幸的是,这对我不起作用。未选择与模型对应的单选按钮。如果我选择一个并提交,它会返回正确的值。
猜你喜欢
  • 2015-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多