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个回答

图片说明

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

caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复weixin_39017744: 截图和代码我看下,你可以发一个新问题,不要悬赏
11 个月之前 回复
weixin_39017744
weixin_39017744 回复caozhy: 这个我是换了的,还是出现我说的错误
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 思路是一样的
11 个月之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 回复weixin_39017744: 你把 <%%>那种写法换成 razor 的 @ 的写法,应该就可以了
11 个月之前 回复
weixin_39017744
weixin_39017744 看起来这个不适用于mvc5,Index 页面显示性别那一行报错:模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。'。create页面radiobutton报错:'未将对象引用设置到对象的实例。'
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问