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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog