如何确定视图中哪个单选按钮处于打开状态?

发布于 2024-08-05 09:32:42 字数 557 浏览 2 评论 0原文

在我的控制器中,我将值设置为 true:

ViewData[itemName] = true;

然后在我看来,我尝试将项目设置为 true 作为选择单选按钮。我尝试过以下方法:

    <%= Html.RadioButton("default",item.Name,
             ((bool)ViewData[item.Name])==null ? false:true) %> 

    <%= Html.RadioButton("default",item.Name,
             ViewData[item.Name]) %> 

    <%= Html.RadioButton("default",item.Name,
             ViewData["defaultItem"]==item.Name) %> 
    //In the case above I assigned ViewData["defaultItem"] = itemName; 

有什么想法吗?

In my controller I am setting my value to true:

ViewData[itemName] = true;

Then in my view I am trying to set the item with true as select radio button. I have tried the following:

    <%= Html.RadioButton("default",item.Name,
             ((bool)ViewData[item.Name])==null ? false:true) %> 

    <%= Html.RadioButton("default",item.Name,
             ViewData[item.Name]) %> 

    <%= Html.RadioButton("default",item.Name,
             ViewData["defaultItem"]==item.Name) %> 
    //In the case above I assigned ViewData["defaultItem"] = itemName; 

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2024-08-12 09:32:42

我根本没有研究过这个特定的问题,但我想我会帮忙压缩一下代码。

为了让下面的内容不那么冗长:

        <% if (ViewData[item.Name] == null)
           { %>
            <%= Html.RadioButton("defaultNAME", item.Name)%>
        <% }
           else
           {%>
           <%= Html.RadioButton("defaultNAME", item.Name, 
                                    (bool)ViewData[item.Name])%> 
        <% } %>

你可以这样做:

<%= ViewData[item.Name] == null ? Html.RadioButton("defaultNAME", item.Name) : Html.RadioButton("defaultNAME", item.Name, (bool)ViewData[item.Name]) %>

我知道这对你的问题没有任何帮助,但由于这是相当简单的代码,我认为这是将这八行或九行压缩成一行的良好候选者。

祝你好运!

编辑:

提交后我意识到这还可以更短。只需要调用一次 Html.RadioButton() 即可。

<% bool defaultValue = false; %>
<%= Html.RadioButton("defaultNAME", item.Name, (bool) (ViewData[item.Name] ?? defaultValue)) %>

使用 null 合并运算符,如果 ViewData[item.Name] 为 null,我们可以获取一些默认值。请随意对默认值进行硬编码,而不是在变量中指定它。我这样做只是为了使示例更清晰。所有三个代码块在语义上都是等效的(尽管考虑到第一个代码块会在 ASP 标记中进出,但应该产生相当不同的 IL)。

I haven't looked in to this particular problem at all, but thought I would lend a hand in compacting the code a bit.

To make the follow a bit less verbose:

        <% if (ViewData[item.Name] == null)
           { %>
            <%= Html.RadioButton("defaultNAME", item.Name)%>
        <% }
           else
           {%>
           <%= Html.RadioButton("defaultNAME", item.Name, 
                                    (bool)ViewData[item.Name])%> 
        <% } %>

You can do this:

<%= ViewData[item.Name] == null ? Html.RadioButton("defaultNAME", item.Name) : Html.RadioButton("defaultNAME", item.Name, (bool)ViewData[item.Name]) %>

I know this doesn't help your question any, but since this is fairly straightforeward code, I think this is a good candidate for squeezing these eight or nine lines down into one.

Good luck!

EDIT:

I realized after submitting that this can be made shorter still. Only one call to Html.RadioButton() is necessary.

<% bool defaultValue = false; %>
<%= Html.RadioButton("defaultNAME", item.Name, (bool) (ViewData[item.Name] ?? defaultValue)) %>

Using the null coalescing operator, we can just grab some default value if ViewData[item.Name] is null. Feel free to hardcode your default value instead of specifying it in a variable. I did that just to make the example clearer. All three code blocks are semantically equivalent (although should produce fairly different IL, considering that the first block drops in and out of ASP markup).

流殇 2024-08-12 09:32:42

在第二个 RadioButton 中,您实际上并未为“IsChecked”指定布尔条件。

它应该看起来像这样:

    <%= Html.RadioButton("default",item.Name,
             ViewData[item.Name] == "something") %> 

In your second RadioButton, you're not actually specifying a boolean condition for "IsChecked."

It should look something like this:

    <%= Html.RadioButton("default",item.Name,
             ViewData[item.Name] == "something") %> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文