加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 单选按钮如何与asp.net mvc绑定一起使用

发布时间:2020-12-16 04:09:43 所属栏目:asp.Net 来源:网络整理
导读:我有一个强类型对象,表示窗体的所有文本框属性,当我发布到控制器时它工作正常. 我现在需要在此表单中添加一个单选按钮.如何映射到强类型对象? 解决方法 如果您使用 HtmlHelper.RadioButton,只要名称与您的属性名称匹配,您就可以了. 以下是我的一个项目的代
我有一个强类型对象,表示窗体的所有文本框属性,当我发布到控制器时它工作正常.

我现在需要在此表单中添加一个单选按钮.如何映射到强类型对象?

解决方法

如果您使用 HtmlHelper.RadioButton,只要名称与您的属性名称匹配,您就可以了.

以下是我的一个项目的代码片段:

<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"),"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"),"normalwidth" } })%><label class="displayinline"><%=DateTime.Now.ToString("dd/MM/yyyy")%></label></span>
<span><%= Html.RadioButton("DateFormat","","normalwidth" } })%><label class="displayinline">custom <%= Html.TextBox("customdate",object> { { "style","width:50px; font-size:12px; display:inline;" } }) %> </label></span>

这是渲染的HTML.请注意,每个输入具有相同的名称,但值不同.只有选中的按钮才会将其值发回服务器.

<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:}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读