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

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")
}


我知道了,我代码没有写Create的逻辑,只写了Edit

public ActionResult Create()
{
return View();
}
你这里返回的是null,没有返回模型
你可以写
public ActionResult Create()
{
return View(new UserGender() { Gender = true });
}