MVC为什么不验证呢?

不懂必须要问 2011-11-25 06:59:47
就算输入空,也不提示的。
怎么回事?
数据模型是生成的。EDMX


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(int categoryID, FormCollection form)
{
var model = Northwind.Categories.FirstOrDefault(c=>c.CategoryID == categoryID);
if (model == null)
{
Categories categories = new Categories();
UpdateModel(categories, new[] { "CategoryName", "Description" });
Northwind.AddToCategories(categories);
Northwind.SaveChanges();
return RedirectToAction("Index");
}
else
return RedirectToAction("Create");

}



<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyFirstMVC.Models.Categories>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Create</h2>
<%= Html.ValidationSummary("Error!") %>
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>

<div class="editor-label">
<%= Html.LabelFor(model => model.CategoryID) %>
</div>
<div class="editor-field">
<%= Html.Hidden("CategoryID") %>
</div>

<div class="editor-label">
<%= Html.LabelFor(model => model.CategoryName) %>
</div>
<div class="editor-field">
<%= Html.TextBox("CategoryName") %>
<%= Html.ValidationMessage("CategoryName", "*") %>
</div>

<div class="editor-label">
<%= Html.LabelFor(model => model.Description) %>
</div>
<div class="editor-field">
<%= Html.TextBox("Description") %>
<%= Html.ValidationMessage("Description", "*") %>
</div>

<p>
<input type="submit" value="Create" />
</p>
</fieldset>

<% } %>

<div>
<%= Html.ActionLink("Back to List", "Index") %>
</div>

</asp:Content>
...全文
182 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
逍遥羽飞 2012-10-26
  • 打赏
  • 举报
回复
EF可以配合[MetadataType(typeof())] 属性来做验证
r01cn 2011-11-26
  • 打赏
  • 举报
回复
我感觉你还是要系统地学一学MVC。在MVC框架中,M(模型)是整个应用程序的核心,MVC应用程序的开发流程一般总是从模型设计开始的,我好象还没有碰到过像你这样,把EF生成的实体数据模型(Entity Data Model)来作为应用程序的模型来使用的。这恐怕也是你验证失效的主要原因。
无水的鱼 2011-11-26
  • 打赏
  • 举报
回复
你判断空的时候有可能它是undefined 所以你应该 if (model == null || model==undefined ) 你试下
不懂必须要问 2011-11-26
  • 打赏
  • 举报
回复
都不明白我问什么。

ADO.NET Entity Data Model这个已经生成类了,我再编写类的话不可能的了。
不懂必须要问 2011-11-26
  • 打赏
  • 举报
回复
自己创建一个MODEL,可以进行验证。但EF生成的怎么验证?
不懂必须要问 2011-11-25
  • 打赏
  • 举报
回复

public partial class Categories : global::System.Data.Objects.DataClasses.EntityObject
{
/// <summary>
/// Create a new Categories object.
/// </summary>
/// <param name="categoryID">Initial value of CategoryID.</param>
/// <param name="categoryName">Initial value of CategoryName.</param>
public static Categories CreateCategories(int categoryID, string categoryName)
{
Categories categories = new Categories();
categories.CategoryID = categoryID;
categories.CategoryName = categoryName;
return categories;
}
/// <summary>
/// There are no comments for Property CategoryID in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int CategoryID
{
get
{
return this._CategoryID;
}
set
{
this.OnCategoryIDChanging(value);
this.ReportPropertyChanging("CategoryID");
this._CategoryID = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value);
this.ReportPropertyChanged("CategoryID");
this.OnCategoryIDChanged();
}
}
private int _CategoryID;
partial void OnCategoryIDChanging(int value);
partial void OnCategoryIDChanged();
/// <summary>
/// There are no comments for Property CategoryName in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute(IsNullable=false)]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.ComponentModel.DataAnnotations.Required(ErrorMessage="*")]
public string CategoryName
{
get
{
return this._CategoryName;
}
set
{
this.OnCategoryNameChanging(value);
this.ReportPropertyChanging("CategoryName");
this._CategoryName = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, false);
this.ReportPropertyChanged("CategoryName");
this.OnCategoryNameChanged();
}
}
private string _CategoryName;
partial void OnCategoryNameChanging(string value);
partial void OnCategoryNameChanged();
/// <summary>
/// There are no comments for Property Description in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
[global::System.ComponentModel.DataAnnotations.Required(ErrorMessage = "*")]
public string Description
{
get
{
return this._Description;
}
set
{
this.OnDescriptionChanging(value);
this.ReportPropertyChanging("Description");
this._Description = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
this.ReportPropertyChanged("Description");
this.OnDescriptionChanged();
}
}
private string _Description;
partial void OnDescriptionChanging(string value);
partial void OnDescriptionChanged();
/// <summary>
/// There are no comments for Property Picture in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmScalarPropertyAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public byte[] Picture
{
get
{
return global::System.Data.Objects.DataClasses.StructuralObject.GetValidValue(this._Picture);
}
set
{
this.OnPictureChanging(value);
this.ReportPropertyChanging("Picture");
this._Picture = global::System.Data.Objects.DataClasses.StructuralObject.SetValidValue(value, true);
this.ReportPropertyChanged("Picture");
this.OnPictureChanged();
}
}
private byte[] _Picture;
partial void OnPictureChanging(byte[] value);
partial void OnPictureChanged();
/// <summary>
/// There are no comments for Products in the schema.
/// </summary>
[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("NorthwindModel", "FK_Products_Categories", "Products")]
[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public global::System.Data.Objects.DataClasses.EntityCollection<Products> Products
{
get
{
return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedCollection<Products>("NorthwindModel.FK_Products_Categories", "Products");
}
set
{
if ((value != null))
{
((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedCollection<Products>("NorthwindModel.FK_Products_Categories", "Products", value);
}
}
}
}
sunnydisk 2011-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 findcaiyzh 的回复:]

Model上加验证了吗?

想这样的
[/Quote]

正解,这样可以用AJAX异步验证了。
不懂必须要问 2011-11-25
  • 打赏
  • 举报
回复
ADO.NET Entity Data Model 已经自动生成模型了。

如何再添加验证呢?
不懂必须要问 2011-11-25
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 findcaiyzh 的回复:]
Model上加验证了吗?

想这样的
[/Quote]

大哥两个儿子啊!强啊。

我用生成的ADO.NET Entity Data Model。也需要写吗?写在那里?
宝_爸 2011-11-25
  • 打赏
  • 举报
回复
可以读下这篇文章

ASP.NET MVC 2: Model Validation
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

ScottGu可是个牛人。
宝_爸 2011-11-25
  • 打赏
  • 举报
回复
Model上加验证了吗?

想这样的

62,046

社区成员

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

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

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

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