ASP.NET MVC 4是ModelState.IsValid,总是false

时间:2014-05-12 21:08:31

标签: c# asp.net-mvc asp.net-mvc-4

问题:我遵循了教程here,我可以从数据库中提取数据。我也可以从数据库中删除记录。但是当我尝试创建或编辑时,我收到了这个错误:

Invalid Value

因此,当我在控制器中查看并且我看到有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")
}

我在这里做错了什么?

3 个答案:

答案 0 :(得分:1)

如果类名和属性名相同,则模型绑定不会正确映射。         [HttpPost]         public ActionResult TextView(Text objtext,string text)         {return View(); } 在这里你可以检查,objText将获取数据,文本也将从文本框中获取数据。 希望它对你有用。

答案 1 :(得分:1)

这可以解决问题: - 在视图底部编码

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

答案 2 :(得分:0)

我认为属性名,类名和参数名都是相同的,搞乱了MVC模型绑定魔法。如果你改变:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Text text)

为:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Text values)

它正确回发。

相关问题