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

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

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

weixin_39017744
weixin_39017744 不过我找到了别的解决方案,谢谢啦
一年多之前 回复
weixin_39017744
weixin_39017744 下面的ddb.UserGenders.Add(userGender);和return View(userGender);直接报错,没有了userGender
一年多之前 回复
caozhy
贵阳老马马善福专业维修游泳池堵漏防水工程 public ActionResult Create([Bind(Include = "Id,Name,Gender")] UserGender userGender) 修改为 public ActionResult Create(FormCollection fc),然后手动更新模型看看
一年多之前 回复
weixin_39017744
weixin_39017744 按照这个写了,点击create按钮后无反应
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐