Costomers och Movies fungerar med validation

This commit is contained in:
2019-01-23 23:18:48 +01:00
parent 3e501b29d0
commit 93e8535819
10 changed files with 267 additions and 23 deletions

View File

@ -41,6 +41,7 @@ namespace Vidly.Controllers
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Customer customer)
{
if (!ModelState.IsValid)

View File

@ -28,15 +28,25 @@ namespace Vidly.Controllers
var movieGenres = _context.MovieGenres.ToList();
var viewModel = new MovieFormViewModel
{
Movie = new Movie(),
//Movie = new Movie(),
MovieGenres = movieGenres
};
return View("MovieForm", viewModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Save(Movie movie)
{
if (!ModelState.IsValid)
{
var viewModel = new MovieFormViewModel(movie)
{
MovieGenres = _context.MovieGenres.ToList()
};
return View("MovieForm", viewModel);
}
if (movie.Id == 0)
_context.Movies.Add(movie);
else
@ -83,9 +93,8 @@ namespace Vidly.Controllers
if (movie == null)
return HttpNotFound();
var viewModel = new MovieFormViewModel
var viewModel = new MovieFormViewModel(movie)
{
Movie = movie,
MovieGenres = _context.MovieGenres.ToList()
};

View File

@ -0,0 +1,29 @@
// <auto-generated />
namespace Vidly.Migrations
{
using System.CodeDom.Compiler;
using System.Data.Entity.Migrations;
using System.Data.Entity.Migrations.Infrastructure;
using System.Resources;
[GeneratedCode("EntityFramework.Migrations", "6.2.0-61023")]
public sealed partial class AddRequiredMarksOnMovieFields : IMigrationMetadata
{
private readonly ResourceManager Resources = new ResourceManager(typeof(AddRequiredMarksOnMovieFields));
string IMigrationMetadata.Id
{
get { return "201901232137372_AddRequiredMarksOnMovieFields"; }
}
string IMigrationMetadata.Source
{
get { return null; }
}
string IMigrationMetadata.Target
{
get { return Resources.GetString("Target"); }
}
}
}

View File

@ -0,0 +1,18 @@
namespace Vidly.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class AddRequiredMarksOnMovieFields : DbMigration
{
public override void Up()
{
AlterColumn("dbo.Movies", "ReleaseDate", c => c.DateTime(nullable: false));
}
public override void Down()
{
AlterColumn("dbo.Movies", "ReleaseDate", c => c.DateTime());
}
}
}

View File

@ -0,0 +1,126 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="Target" xml:space="preserve">
<value>H4sIAAAAAAAEAO1d3W7cuhG+L9B3EHTVFj5e/zRBauyeA2cdt0ZjO8g6Qe8MrkSvhUjUHonr2CjOk/Wij9RXKKlf/ovSarVycBAgsEVyOBx+Qw6HnPH//vPf6S/PUeg8wSQNYjRzjw+PXAciL/YDtJq5G/zw0zv3l5//+IfpBz96dr6W9U5pPdISpTP3EeP12WSSeo8wAulhFHhJnMYP+NCLownw48nJ0dHfJsfHE0hIuISW40w/bxAOIpj9Qn6dx8iDa7wB4XXswzAtvpOSRUbVuQERTNfAgzP3a+CHL4d5Pdc5DwNAeFjA8MF1AEIxBphwePYlhQucxGi1WJMPILx7WUNS7wGEKSw4P6ur2w7i6IQOYlI3LEl5mxTHUUuCx6eFVCZi806ydSupEbl9IPLFL3TUmexm7jzrAiauI3Z2Ng8TWpEX7WHZ4MDJPh9Uk08wQv8dOPNNiDcJnCG4wQkID5xPm2UYeP+EL3fxN4hmaBOGLFOELVLGfSCfPiXxGib45TN8KFi98l1nwrebiA2rZkybfBBXCJ+euM4N6RwsQ1jNOTPgBY4T+HeIYAIw9D8BjGGCKA2YSU3qXeiL/l/2RkBGNMV1rsHzR4hW+HHmnrx54zqXwTP0yy8FB19QQBSLNMLJBio4NPf6Pkjw4wVhuOya/nwXRI0Nr9LFZpl6SbCE/l18A7+nHyEdcUnnfRyHEKDWDF3DaEkg8RisKZ16At6/YIvR3YCnYJVNh5Gs63yGYVaNfskVvYLmvVj3Momjz3HIwF2ocr+IN4lHRRib692BZAUxz/V0UmuVUddEtiw1jm/22vTObtrbqdKbIytNMvexCFboy/oSQmaFOH7bmtWLTZJN3hW6jhF+3GrYF0HqxWQL/MyoswUhewDGTwFd4JIW4Kua/A48IkDw3Bp57Wan3cS8tjn5UTdhshlBkEL1Ntx2ESANz30f+m039JsN3S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xDIbvvo+4gl9K/Q7QavN5gMmew6IXcTRi1xU/9Z3hqe5+ntmv6W9jEEwmZAb/Rv0ftNEPoV35eKyyINCWriF/fAdC4xvQ9evVSUbmJkSagQX3UyuYPROiTE0lu0AE+wC28Efh/hCngv9dWgjkjzRPBin14EYJWAKC1o1O3JrwTDfvT88/8BTWb0R7uBAAA=</value>
</data>
<data name="DefaultSchema" xml:space="preserve">
<value>dbo</value>
</data>
</root>

View File

@ -14,11 +14,14 @@ namespace Vidly.Models
[StringLength(255)]
public string Name { get; set; }
[Required]
[Display(Name="Release Date")]
public DateTime? ReleaseDate { get; set; }
public DateTime? DateAdded { get; set; }
[Required]
[Range(1,20)]
[Display(Name = "Number In Stock")]
public int NumberInStock { get; set; }

View File

@ -241,6 +241,10 @@
<Compile Include="Migrations\201901222227126_AddedNullableAddedDateToMovie.Designer.cs">
<DependentUpon>201901222227126_AddedNullableAddedDateToMovie.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\201901232137372_AddRequiredMarksOnMovieFields.cs" />
<Compile Include="Migrations\201901232137372_AddRequiredMarksOnMovieFields.Designer.cs">
<DependentUpon>201901232137372_AddRequiredMarksOnMovieFields.cs</DependentUpon>
</Compile>
<Compile Include="Migrations\Configuration.cs" />
<Compile Include="Models\AccountViewModels.cs" />
<Compile Include="Models\Customer.cs" />
@ -388,6 +392,9 @@
<EmbeddedResource Include="Migrations\201901222227126_AddedNullableAddedDateToMovie.resx">
<DependentUpon>201901222227126_AddedNullableAddedDateToMovie.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Migrations\201901232137372_AddRequiredMarksOnMovieFields.resx">
<DependentUpon>201901232137372_AddRequiredMarksOnMovieFields.cs</DependentUpon>
</EmbeddedResource>
</ItemGroup>
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using Vidly.Models;
@ -9,6 +10,44 @@ namespace Vidly.ViewModels
public class MovieFormViewModel
{
public IEnumerable<MovieGenre> MovieGenres { get; set; }
public Movie Movie { get; set; }
public int? Id { get; set; }
[Required]
[StringLength(255)]
public string Name { get; set; }
[Required]
[Display(Name = "Release Date")]
public DateTime? ReleaseDate { get; set; }
[Required]
[Range(1, 20)]
[Display(Name = "Number In Stock")]
public int? NumberInStock { get; set; }
[Display(Name = "Genre")]
[Required]
public byte? MovieGenreId { get; set; }
public string Title
{
get
{
return (Id != 0) ? "Edit Movie":"New Movie";
}
}
public MovieFormViewModel()
{
Id = 0;
}
public MovieFormViewModel(Movie movie)
{
Id = movie.Id;
Name = movie.Name;
ReleaseDate = movie.ReleaseDate;
NumberInStock = movie.NumberInStock;
MovieGenreId = movie.MovieGenreId;
}
}
}

View File

@ -17,7 +17,7 @@ else
@using (@Html.BeginForm("Save", "Customers"))
{
@Html.ValidationSummary(true, "Please fix the following errors.")
@Html.ValidationSummary(true, "Var snäll och åtgärda felen.")
<div class="form-group">
@Html.LabelFor(m => m.Customer.Name)
@Html.TextBoxFor(m => m.Customer.Name, new { @class = "form-control" })
@ -39,6 +39,12 @@ else
</label>
</div>
@Html.HiddenFor(m => m.Customer.Id)
@Html.AntiForgeryToken();
<button type="submit" class="btn btn-primary">Save</button>
}
@section scripts
{
@Scripts.Render("~/bundles/jqueryval")
}

View File

@ -5,33 +5,39 @@
}
@if (Model.Movie.Id == null || Model.Movie.Id == 0)
{
<h2>New Movie</h2>
}
else
{
<h2>Edit Movie</h2>
}
<h2>@Model.Title</h2>
@using (Html.BeginForm("Save", "Movies"))
{
@Html.ValidationSummary(true, "Var snäll och åtgärda felen.")
<div class="form-group">
@Html.LabelFor(m => m.Movie.Name)
@Html.TextBoxFor(m => m.Movie.Name, new { @class = "form-control" })
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.Name)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Movie.ReleaseDate)
@Html.TextBoxFor(m => m.Movie.ReleaseDate, "{0:d MMM yyyy}", new { @class = "form-control" })
@Html.LabelFor(m => m.ReleaseDate)
@Html.TextBoxFor(m => m.ReleaseDate, "{0:d MMM yyyy}", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.ReleaseDate)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Movie.MovieGenreId)
@Html.DropDownListFor(m => m.Movie.MovieGenreId, new SelectList(Model.MovieGenres, "Id", "Name"), "Select Genre Type", new { @class = "form-control" })
@Html.LabelFor(m => m.MovieGenreId)
@Html.DropDownListFor(m => m.MovieGenreId, new SelectList(Model.MovieGenres, "Id", "Name"), "Select Genre Type", new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.MovieGenreId)
</div>
<div class="form-group">
@Html.LabelFor(m => m.Movie.NumberInStock)
@Html.TextBoxFor(m => m.Movie.NumberInStock, new { @class = "form-control" })
@Html.LabelFor(m => m.NumberInStock)
@Html.TextBoxFor(m => m.NumberInStock, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.NumberInStock)
</div>
@Html.HiddenFor(m=>m.Movie.Id)
@Html.HiddenFor(m => m.Id)
@Html.AntiForgeryToken();
<button type="submit" class="btn btn-primary">Save</button>
}
}
@section scripts
{
@Scripts.Render("~/bundles/jqueryval")
}