繁体   English   中英

如何获取单选按钮组的ID?

[英]How can I get the Id of my radio button group?

我有许多名为Properties的单选按钮组。 属性可以是诸如“颜色”,“大小”,“高度”之类的东西。 这些属性具有选项。 对于“颜色”,选项可以是“红色”,“蓝色”,依此类推。 发布表单时,我可以读取每个单选按钮的Id ,但是无法访问组Id

我正在渲染这样的属性:

for (int i = 0; i < Model.Properties.Count(); i++)
{
    <div class="col-lg-3 col-md-4 col-sm-6">
        <strong>@Model.Properties[i].Label</strong><br />
        @foreach (var option in Model.Properties[i].Options)
        {
            <label style="font-weight:unset;">
                @Html.RadioButtonFor(m => m.Properties[i].SelectedRadioOption, option.Id, new { id = Model.Properties[i].Id })
                @option.Value
                @Model.Properties[i].Unit
            </label>
            <br />
        }
        @Html.ValidationMessageFor(m => m.Properties[i].SelectedRadioOption)
    </div>
}

这样会产生如下HTML:

<div class="col-lg-3 col-md-4 col-sm-6">
    <strong>Size</strong><br />
    <label style="font-weight:unset;">
        <input checked="checked" data-val="true" data-val-required="The Selected property field is required." id="7012" name="Properties[0].SelectedRadioOption" type="radio" value="7025" />
        34 - 36
    </label>
    <br />
    <label style="font-weight:unset;">
        <input id="7012" name="Properties[0].SelectedRadioOption" type="radio" value="7026" />
        37 - 39
    </label>
    <br />
    <span class="field-validation-valid" data-valmsg-for="Properties[0].SelectedRadioOption" data-valmsg-replace="true"></span>                                
</div>
<div class="col-lg-3 col-md-4 col-sm-6">
    <strong>Colour</strong><br />
    <label style="font-weight:unset;">
        <input data-val="true" data-val-required="The Selected property field is required." id="7013" name="Properties[1].SelectedRadioOption" type="radio" value="7029" />
        Black
    </label>
    <br />
    <label style="font-weight:unset;">
        <input id="7013" name="Properties[1].SelectedRadioOption" type="radio" value="7036" />
        Pink
    </label>
    <br />
    <span class="field-validation-valid" data-valmsg-for="Properties[1].SelectedRadioOption" data-valmsg-replace="true"></span>
</div>

这是控制器的POST方法中的部分,在其中我遍历Properties

List<PropertyOptionForProduct> propertyOptions = new List<PropertyOptionForProduct>();
if (VMProduct.Properties != null)
{
    for (var i = 0; i < VMProduct.Properties.Count(); i++)
    {
        propertyOptions.Add(new PropertyOptionForProduct
        {
            ProductId = VMProduct.Id,
            ProductPropertyId = VMProduct.Properties[i].Id, // <-- This is always 0!
            ProductPropertyOptionId = (int)VMProduct.Properties[i].SelectedRadioOption
        });
    }
}

如果在“颜色”属性上选择“黑色”,则对象看起来像这样:

PropertyOptionForProduct
{
    ProductId = 17008,
    ProductPropertyId = 0, // <-- Why not 7013?
    ProductPropertyOptionId = 7025
}

上例中ProductPropertyId的正确值应该是7013,它是单选按钮标记上的id -value。

我究竟做错了什么?

从html <form>发回的内容的格式将是(例如) name1=value1&name2=value2它映射到您选中的单选按钮的名称和值字段。 例如,您的代码可能生成了此帖子Properties[0].SelectedRadioOption=7026&Properties[1].SelectedRadioOption=7036

服务器不知道单选按钮html中的id属性。 除非您在发送之前更改了帖子(使用javascript),否则ID字段不包含在帖子中。 因此,要么需要在值字段中包括所有标识符,要么像value="1243;34534"这样分隔开,或者需要在发布之前使用JavaScript将ID包含在发布数据中。

原来,我是菜鸟。

我需要添加的所有内容使其成为视图中的这一行剃刀代码:

@Html.HiddenFor(m => m.Properties[i].Id)

现在,我可以在控制器中访问Properties[i].Id的值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM