weixin_39017744 2019-03-17 23:04 采纳率: 83.3%
浏览 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条回答

  • threenewbee 2019-03-18 00:07
    关注

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

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

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

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试