[英]ASP.NET MVC 4 is ModelState.IsValid always false
问题:我在这里按照教程,我可以从数据库中提取数据。 我也可以从数据库中删除记录。 但是当我尝试创建或编辑时,我收到此错误:
因此,当我查看控制器时,我发现有ModelState.IsValid就是当我为它设置一个断点时,它总是假的。
这是我的模型:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class Text
{
[Key]
public int id { get; set; }
[Required]
public string text { get; set; }
}
public class textDBContext : DbContext
{
public DbSet<Text> texts { get; set; }
}
}
我的数据库有两个字段:“id”和“text”。 “id”的数据类型为int,“text”的数据类型为字符串。
我从模型中生成了这个控制器,它也生成了视图(CRUD)
这是我的控制器:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class textDBController : Controller
{
private textDBContext db = new textDBContext();
//
// GET: /textDB/
public ActionResult Index()
{
return View(db.texts.ToList());
}
//
// GET: /textDB/Details/5
public ActionResult Details(int id = 0)
{
Text text = db.texts.Find(id);
if (text == null)
{
return HttpNotFound();
}
return View(text);
}
//
// GET: /textDB/Create
public ActionResult Create()
{
return View();
}
//
// POST: /textDB/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Text text)
{
if (ModelState.IsValid)
{
db.texts.Add(text);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(text);
}
//
// GET: /textDB/Edit/5
public ActionResult Edit(int id = 0)
{
Text text = db.texts.Find(id);
if (text == null)
{
return HttpNotFound();
}
return View(text);
}
//
// POST: /textDB/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Text text)
{
if (!ModelState.IsValid)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
// Breakpoint, Log or examine the list with Exceptions.
}
if (ModelState.IsValid)
{
db.Entry(text).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(text);
}
//
// GET: /textDB/Delete/5
public ActionResult Delete(int id = 0)
{
Text text = db.texts.Find(id);
if (text == null)
{
return HttpNotFound();
}
return View(text);
}
//
// POST: /textDB/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Text text = db.texts.Find(id);
db.texts.Remove(text);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
这是编辑视图:
@model MvcApplication1.Models.Text
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Text</legend>
@Html.HiddenFor(model => model.id)
<div class="editor-label">
@Html.LabelFor(model => model.text)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.text)
@Html.ValidationMessageFor(model => model.text)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我在这做错了什么?
如果类名和属性名相同,则模型绑定不会正确映射。 [HttpPost] public ActionResult TextView(Text objtext,string text){return View(); 在这里你可以检查,objText将获取数据,文本也将从文本框中获取数据。 希望它能为你效劳。
这可以解决问题: - 在View的底部编码
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我认为属性名称,类名和参数名都是相同的,搞乱了MVC模型绑定魔法。 如果你改变:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Text text)
至:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Text values)
它正确回发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.