weixin_39017744 2019-03-17 23:04 采纳率: 50%
浏览 639
已采纳

ASP.NET MVC5性别设置为bool,把根据EF生成的代码中checkbox改成radiobutton后的报错问题

这里是报错的图
图片说明

Model代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RadioButtonTest1.Models
{
    public class UserGender
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool Gender { get; set; }
    }
}

Controller代码未修改

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RadioButtonTest1.Models;

namespace RadioButtonTest1.Controllers
{
    public class UserGendersController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: UserGenders
        public ActionResult Index()
        {
            return View(db.UserGenders.ToList());
        }

        // GET: UserGenders/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserGender userGender = db.UserGenders.Find(id);
            if (userGender == null)
            {
                return HttpNotFound();
            }
            return View(userGender);
        }

        // GET: UserGenders/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: UserGenders/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "Id,Name,Gender")] UserGender userGender)
        {
            if (ModelState.IsValid)
            {
                db.UserGenders.Add(userGender);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(userGender);
        }

        // GET: UserGenders/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserGender userGender = db.UserGenders.Find(id);
            if (userGender == null)
            {
                return HttpNotFound();
            }
            return View(userGender);
        }

        // POST: UserGenders/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "Id,Name,Gender")] UserGender userGender)
        {
            if (ModelState.IsValid)
            {
                db.Entry(userGender).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(userGender);
        }

        // GET: UserGenders/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            UserGender userGender = db.UserGenders.Find(id);
            if (userGender == null)
            {
                return HttpNotFound();
            }
            return View(userGender);
        }

        // POST: UserGenders/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            UserGender userGender = db.UserGenders.Find(id);
            db.UserGenders.Remove(userGender);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

Create页面代码(这里之前的checkbox被改成了RadioButton)

@model RadioButtonTest1.Models.UserGender

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>


@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()

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

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

                @Html.RadioButton("gender", "male", Model.Gender) Male
                @Html.RadioButton("gender", "female", !Model.Gender) Fmale


            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}


  • 写回答

1条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 关于#glmb#的问题,如何解决?
      • ¥15 TI的TMS320F28335,RS485串口进行SCI-B通信出错
      • ¥15 XML文件报错不允许出现此特性,百度也搜不到解决方法
      • ¥15 FTPC共同交流学习
      • ¥15 关于#python##pyqt#的问题,如何解决?
      • ¥15 html+css网页设计 html+css网页设计
      • ¥15 用html加css制作一个网页
      • ¥200 MFC中如何对ListCtrl的某一列的文本能换行显示
      • ¥15 clion添加库文件
      • ¥15 ERROR C2143怎么出现的?为什么ERROR C2144会这么转移,它是怎么出现的?该怎么解决?