62,046
社区成员
发帖
与我相关
我的任务
分享
[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>
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);
}
}
}
}