ASP.net MVC运行添加数据出错

usstcoin 2015-03-18 02:13:07
初学者的问题,请大牛帮忙看看,不胜感激。
通过model创建controller,同步创建Create等view.以及数据库上下文。但运行create时出错



Model
namespace MvcApplication7.Models
{
public class Mymodel
{
[Key]
public int Id { get; set; }

[Required]
[MaxLength(20)]
public string Name { get; set; }

[Required]
[MaxLength(30)]
public string StandardName { get; set; }

[Required]
[MaxLength(4)]
public int Sigma_b { get; set; }
}
}


Controller
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication7.Models;

namespace MvcApplication7.Controllers
{
public class Default1Controller : Controller
{
private MvcApplication7Context db = new MvcApplication7Context();
public ActionResult Index()
{
return View(db.Mymodels.ToList());
}
public ActionResult Details(int id = 0)
{
Mymodel mymodel = db.Mymodels.Find(id);
if (mymodel == null)
{
return HttpNotFound();
}
return View(mymodel);
}
public ActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Mymodel mymodel)
{
if (ModelState.IsValid)
{
db.Mymodels.Add(mymodel);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(mymodel);
}
}
}


数据库上下文
public class MvcApplication7Context : DbContext
{
public MvcApplication7Context() : base("name=MvcApplication7Context")
{
}

public DbSet<Mymodel> Mymodels { get; set; }
}


...全文
185 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zujinsheng 2015-03-18
  • 打赏
  • 举报
回复
int 类型没有长度, 只有范围..
引用 5 楼 usstcoin 的回复:
[quote=引用 4 楼 lc8882972 的回复:] 把MaxLength属性去了看看
嗯,这样管用,谢谢 那再请问,为什么设置长度限制会出错 还有,int 类型怎么限制长度?[/quote]
usstcoin 2015-03-18
  • 打赏
  • 举报
回复
引用 4 楼 lc8882972 的回复:
把MaxLength属性去了看看
嗯,这样管用,谢谢 那再请问,为什么设置长度限制会出错 还有,int 类型怎么限制长度?
LcGyAn 2015-03-18
  • 打赏
  • 举报
回复
把MaxLength属性去了看看
usstcoin 2015-03-18
  • 打赏
  • 举报
回复
数据库是Entity Framework用模型自动生成的,在LocalDb服务器。这里是int类型啊
勇敢的老散户 2015-03-18
  • 打赏
  • 举报
回复
你输入了int32类型,数据库是你后面那个类型,改成int32就好了
Justin-Liu 2015-03-18
  • 打赏
  • 举报
回复
问题很明确啊 把model和数据库的字段类型对比一下

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧