如何单选按钮与asp.net mvc的工作结合 [英] how do radio buttons work with asp.net mvc binding

查看:139
本文介绍了如何单选按钮与asp.net mvc的工作结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个强类型的对象,再presents所有窗体的文本框属性,当我张贴到控制器正常工作。

i have an strongly typed object that represents all of the textbox properties of a form and it works fine when i post to the controller.

我现在需要一个单选按钮添加到该表单。这是如何映射到强类型对象?

i now need to add a radio button to this form. how does that map to the strongly typed objects?

推荐答案

如果您使用HtmlHelper.RadioButton你会被罚款,只要你的名字属性名相匹配。

If you use the HtmlHelper.RadioButton you will be fine as long as the name matches your property name.

下面是code从我的项目的一个片段:

Below is a snippet of code from one of my projects:

<span><%= Html.RadioButton("DateFormat", "MMMM/dd/yy", Model.DateFormat.Equals("MMMM/dd/yy"), new Dictionary<string, object> { { "class", "normalwidth" } })%><label class="displayinline"><%=DateTime.Now.ToString("MMMM dd, yyyy")%></label></span>
<span><%= Html.RadioButton("DateFormat", "yyyy/MM/dd", Model.DateFormat.Equals("yyyy/MM/dd"), new Dictionary<string, object> { { "class", "normalwidth" } })%><label class="displayinline"><%=DateTime.Now.ToString("yyyy/MM/dd")%></label></span>
<span><%= Html.RadioButton("DateFormat", "dd/MM/yyyy", Model.DateFormat.Equals("dd/MM/yyyy"), new Dictionary<string, object> { { "class", "normalwidth" } })%><label class="displayinline"><%=DateTime.Now.ToString("dd/MM/yyyy")%></label></span>
<span><%= Html.RadioButton("DateFormat", "", new Dictionary<string, object> { { "class", "normalwidth" } })%><label class="displayinline">custom <%= Html.TextBox("customdate", "", new Dictionary<string, object> { { "style", "width:50px; font-size:12px; display:inline;" } }) %> </label></span>

这里是呈现的HTML。注意,每个输入具有相同的名称,但不同的值。只有选定按钮将它回发到服务器值。

And here is the rendered HTML. Notice each input has the same name, but different values. Only the selected button will have it's value posted back to the server.

    <p><label>Date Format</label> 
        <span><input class="normalwidth" id="DateFormat" name="DateFormat" type="radio" value="MMMM/dd/yy" /><label class="displayinline">October 18, 2009</label></span> 
        <span><input checked="checked" class="normalwidth" id="DateFormat" name="DateFormat" type="radio" value="yyyy/MM/dd" /><label class="displayinline">2009/10/18</label></span> 
        <span><input class="normalwidth" id="DateFormat" name="DateFormat" type="radio" value="dd/MM/yyyy" /><label class="displayinline">18/10/2009</label></span> 
        <span><input class="normalwidth" id="DateFormat" name="DateFormat" type="radio" value="" /><label class="displayinline">custom <input id="customdate" name="customdate" style="width:50px; font-size:12px; display:inline;" type="text" value="" /> </label></span> 
    </p>

和在你的类:

public class Post
{
   public string DateFormat {get; set:}
}

这篇关于如何单选按钮与asp.net mvc的工作结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆