someone thinks the drop-down list is not as good as the radio button, I have to test the radio button and the drop-down box. The test code is as follows:
1.model class Blog.cs (types using enumeration types, automatically generated views are shown in the drop-down list):
usingSystem.ComponentModel; usingSystem.ComponentModel.DataAnnotations; namespaceWebtest.models { Public enumB_type {Original, reproduced, translated} Public classBlog {[Key] Public intb_id {Get;Set; } [DisplayName ("title")] Public stringB_title {Get;Set; } [DisplayName ("content")] Public stringb_content {Get;Set; } [DisplayName ("type")] PublicB_type B_type {Get;Set; } [DisplayName ("label")] Public stringB_tag {Get;Set; } } }
2. Add a string to the connection database in Web. config (SQL Server database is recommended, or localdb with VS comes with). This step will not be a good way to see the introductory tutorial, lazy to write steps, and then the shortcut key Ctrl+shift+b build the solution, and then create a new controller with the view.
3. Modify the automatically generated create.cshtml view code as follows (take a closer look at the change section):
<divclass="Form-group">@Html. Labelfor (Model= Model. B_type, Htmlattributes:New{@class ="Control-label col-md-2" }) <divclass="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> <divclass="Form-group">@Html. Labelfor (Model= Model. B_tag, Htmlattributes:New{@class ="Control-label col-md-2" }) <divclass="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. In the browser open create.cshtml view, create a new data, the system will automatically create a database when you add new data to the database content
5. Modify the Edit.cshtml View Code as follows (note that the contents of the database store are automatically selected when the view is rendered):
<divclass="Form-group">@Html. Labelfor (Model= Model. B_tag, Htmlattributes:New{@class ="Control-label col-md-2" }) <divclass="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. If you do it again, you will have a clearer picture of the process, which corresponds to changing your own project. Now I still can't tell which is beautiful, but from the implementation, the drop-down list is easier to implement, just define a field in the model class as an enumeration type, you don't have to change the code.
MVC5 using the radio button and the drop-down box "Go"