weixin_39017744 2019-03-17 01:35 采纳率: 83.3%
浏览 438
已采纳

ASP.NET MVC5项目中,如何使用rediobutton插入数据

比如我想添加性别数据,用rediobutton。EF原句如下:

    <div class="form-group">
            @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Gender, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Gender, "", new { @class = "text-danger" })
            </div>
        </div>

怎么把它改成用rediobutton选性别?

  • 写回答

1条回答

  • threenewbee 2019-03-17 15:35
    关注

    图片说明

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using Q752345.Models;
    
    namespace Q752345.Controllers
    {
        public class PersonController : Controller
        {
            //
            // GET: /Person/
    
            static List<PersonViewModel> model = new List<PersonViewModel>();
    
            static PersonController()
            {
                model.Add(new PersonViewModel() { ID = 1, Name = "aaa", Gender = true });
                model.Add(new PersonViewModel() { ID = 2, Name = "bbb", Gender = true });
                model.Add(new PersonViewModel() { ID = 3, Name = "ccc", Gender = false });
            }
    
            public ActionResult Index()
            {
                return View(model);
            }
    
            //
            // GET: /Person/Details/5
    
            public ActionResult Details(int id)
            {
                return View(model.Single(x => x.ID == id));
            }
    
            //
            // GET: /Person/Create
    
            public ActionResult Create()
            {
                return View();
            } 
    
            //
            // POST: /Person/Create
    
            [HttpPost]
            public ActionResult Create(FormCollection collection)
            {
                try
                {
                    // TODO: Add insert logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            //
            // GET: /Person/Edit/5
    
            public ActionResult Edit(int id)
            {
                return View(model.Single(x => x.ID == id));
            }
    
            //
            // POST: /Person/Edit/5
    
            [HttpPost]
            public ActionResult Edit(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add update logic here
                    var p = model.Single(x => x.ID == id);
                    p.Name = Request.Form["Name"];
                    p.Gender = Request.Form["Gender"].ToString().ToLower() == "male";
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
    
            //
            // GET: /Person/Delete/5
    
            public ActionResult Delete(int id)
            {
                return View();
            }
    
            //
            // POST: /Person/Delete/5
    
            [HttpPost]
            public ActionResult Delete(int id, FormCollection collection)
            {
                try
                {
                    // TODO: Add delete logic here
    
                    return RedirectToAction("Index");
                }
                catch
                {
                    return View();
                }
            }
        }
    }
    
    
    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Q752345.Models.PersonViewModel>" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
        Edit
    </asp:Content>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <h2>Edit</h2>
    
        <% using (Html.BeginForm()) {%>
            <%: Html.ValidationSummary(true) %>
    
            <fieldset>
                <legend>Fields</legend>
    
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.ID) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.ID) %>
                    <%: Html.ValidationMessageFor(model => model.ID) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Name) %>
                </div>
                <div class="editor-field">
                    <%: Html.TextBoxFor(model => model.Name) %>
                    <%: Html.ValidationMessageFor(model => model.Name) %>
                </div>
    
                <div class="editor-label">
                    <%: Html.LabelFor(model => model.Gender) %>
                </div>
                <div class="editor-field">
    <%--                <%: Html.TextBoxFor(model => model.Gender) %>
                    <%: Html.ValidationMessageFor(model => model.Gender) %>--%>
                    <%: Html.RadioButton("gender", "male", Model.Gender)%>男
                    <%: Html.RadioButton("gender", "female", !Model.Gender)%>女
                </div>
    
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>
    
        <% } %>
    
        <div>
            <%: Html.ActionLink("Back to List", "Index") %>
        </div>
    
    </asp:Content>
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Q752345.Models
    {
        public class PersonViewModel
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public bool Gender { get; set; }
        }
    }
    

    完整代码:https://download.csdn.net/download/caozhy/11029276

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题