MVC5使用单选按钮与下拉框【转】

摘要:
有人认为下拉列表的呈现不如单选按钮漂亮,所以我必须测试单选按钮和下拉框。在这一步中,您不必只阅读入门教程,不必费心编写步骤),然后按Ctrl+Shift+B生成解决方案,然后创建带有视图的新控制器。现在我仍然无法分辨哪个是漂亮的,但在实现方面,下拉列表更容易实现。只需将模型类中的字段定义为枚举类型,就不需要更改代码。

某人认为下拉列表的呈现形式不如单选按钮漂亮,我只好去测试一下单选按钮与下拉框了。测试代码如下:

1.model类Blog.cs(类型使用枚举类型,自动生成的视图会以下拉列表形式显示):

using System.ComponentModel;  
using System.ComponentModel.DataAnnotations;  
  
namespace WebTest.Models  
{  
    public enum B_Type  
    {  
        原创,转载,翻译  
    }  
    public class Blog  
    {  
        [Key]   
        public int B_Id { get; set; }  
          
        [DisplayName("标题")]  
        public string B_Title { get; set; }  
  
        [DisplayName("内容")]  
        public string B_Content { get; set; }   
          
        [DisplayName("类型")]  
        public B_Type B_Type { get; set; }  
  
        [DisplayName("标签")]  
        public string B_Tag { get; set; }  
    }  
}  

2.在web.config添加连接数据库的字符串(推荐使用sql server数据库,或者使用vs自带的localdb。这一步不会就乖乖去看入门教程,懒得写步骤),然后快捷键ctrl+shift+B 生成解决方案,然后新建带视图的控制器。

 

3.修改自动生成的Create.cshtml视图代码如下(仔细看一下更改部分):

 

<div class="form-group">  
          @Html.LabelFor(model => model.B_Type, htmlAttributes: new { @class = "control-label col-md-2" })  
          <div class="col-md-10">  
            @Html.EnumDropDownListFor(model => model.B_Type, htmlAttributes: new { @class = "form-control" })</span>  
              @Html.ValidationMessageFor(model => model.B_Type, "", new { @class = "text-danger" })  
          </div>  
      </div>  
  
      <div class="form-group">  
          @Html.LabelFor(model => model.B_Tag, htmlAttributes: new { @class = "control-label col-md-2" })  
          <div class="col-md-10">  
                @*@Html.EditorFor(model => model.B_Tag, new { htmlAttributes = new { @class = "form-control" } })*@  
              @Html.RadioButtonFor(model=>model.B_Tag, "c#",new { htmlAttributes = new { @class = "form-control" } })C#  
              @Html.RadioButtonFor(model => model.B_Tag, "java", new { htmlAttributes = new { @class = "form-control" } })Java  
              @Html.RadioButtonFor(model => model.B_Tag, "python", new { htmlAttributes = new { @class = "form-control" } })Python</span>  
              @Html.ValidationMessageFor(model => model.B_Tag, "", new { @class = "text-danger" })  
          </div>  
      </div>  

4.在浏览器打开Create.cshtml视图,新建一条数据,系统在自动创建数据库时会往数据库添加你新建的数据内容

 

5.修改Edit.cshtml视图代码如下(注意观察不同,视图呈现时会自动选中数据库存储的内容的):

<div class="form-group">  
    @Html.LabelFor(model => model.B_Tag, htmlAttributes: new { @class = "control-label col-md-2" })  
    <div class="col-md-10">  
    @*@Html.EditorFor(model => model.B_Tag, new { htmlAttributes = new { @class = "form-control" } })*@  
        @Html.RadioButtonFor(model => model.B_Tag, "c#", new { htmlAttributes = new { @class = "form-control" } })C#  
        @Html.RadioButtonFor(model => model.B_Tag, "java", new { htmlAttributes = new { @class = "form-control" } })Java</span>  
        @Html.RadioButtonFor(model => model.B_Tag, "python", new { htmlAttributes = new { @class = "form-control" } })Python  
        @Html.ValidationMessageFor(model => model.B_Tag, "", new { @class = "text-danger" })  
    </div>  
</div> 

6.如果你做了一遍,就会对流程比较清楚了,对应着改自己的项目即可。现在我还是分不清哪个漂亮,不过从实现上讲,下拉列表更易实现,只需将model类中的某一字段定义为枚举类型,就不必你改代码了呀。

 

免责声明:文章转载自《MVC5使用单选按钮与下拉框【转】》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇DevExpress WPF界面控件Linux多进程编程下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

修复数据库索引问题:删除索引以提升性能

在一个数据库上创建索引会给数据库带来负面影响。当对表执行插入、更新和删除操作时,您就会看到这个性能的负面影响。您对表每作一次修改,包含这些修改记录的索引都必须更新,以符合最新的修改。  使用过滤索引后,需要更新的索引变少了。然而,包含这些记录的索引仍然需要在记录修改时进行更新。   因为这些操作必须在每一次数据修改时进行,您应该把数据库中不用于查询数据的索...

一些基本的Oracle命令

一些基本的Oracle命令基本命令 连接数据库 C:>SQLPLUS /NOLOG SQL>CONN / AS SYSDBA 1.Oracle 关闭 SQL>SHUTDOWN (ABORT|IMMEDIATE|NORMAL) 2.Oracle 启动 SQL>STARTUP (REMOUNT|MOUNT|OPENT) 3.SQL>...

jquery动态实现填充下拉框

 当点下拉框时动态加载后台数据。 后台代码 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8...

selenium 通过xpath进行元素定位--By.xpath()

xpath是XML路径语言,它可以用来确定xml文档中的元素位置,通过元素的路径来完成对元素的查找。HTML就是XML的一种实现方式,所以xpath是一种非常强大的定位方式。xpath也分几种不同类型的定位方法。 一种是绝对路径定位。这种定位方式是利用html标签名的层级关系来定位元素的绝对路径,一般从<html>标签开始依次往下进行查找。如百...

psql 命令行使用

如果觉得直接打开数据库修改繁琐,那么使用终端命令行是方便而又高大上的。下面来看看有哪些命令行:  说明:如果是正式的服务器则需要进行一个操作在执行下面的命令 ssh name @主机地址 -- name 是用户名 主机地址可以是ip地址或者域名 然后输入密码 登录成功后方可用下面的命令 (1)使用命令行连接数据库 psql -U postgres -h...

【干货】Kafka 事务特性分析

特性背景 消息事务是指一系列的生产、消费操作可以要么都完成,要么都失败,类似数据库的事务。这个特性在0.10.2的版本是不支持的,从0.11版本开始才支持。华为云DMS率先提供Kafka 1.1.0的专享版服务,支持消息事务特性。       支持事务消息有什么作用?消息事务是实现分布式事务的一种方案,可以确保分布式场景下的数据最终一致性。例如最常用的转账...