I am using ASP.Net Corre 3.1. My Model is like this-
public class Review: BaseEntity
{
[Required]
[Range(1,10)]
public float Rating { get; set; }
[Required(ErrorMessage = "Movie Watch date should be given")]
[Display(Name = "Date of Watch", Prompt = "Please Give Date of Watch the Movie")]
public DateTime WatchDate { get; set; }
//public virtual string UserId { get; set; } //Not Needed because they are automatically handled by Dot.Net Core
[Required]
[ScaffoldColumn(false)]
public virtual User User { get; set; }
//public virtual string MovieId { get; set; } //Not Needed because they are automatically handled by Dot.Net Core
[Required]
public virtual Movie Movie { get; set; }
}
BaseEntity is like this-
public class BaseEntity
{
//[MaxLength(255)]
[Key, Required, Column(TypeName = "VARCHAR(255)")]
[ScaffoldColumn(false)]
public Guid Id { get; set; }
[DataType(DataType.DateTime)]
[ScaffoldColumn(false)]
public DateTime CreatedDate { get; set; }
[DataType(DataType.DateTime)]
[ScaffoldColumn(false)]
//[DatabaseGenerated(DatabaseGeneratedOption.Computed)] //Not Needed
public DateTime? UpdatedDate { get; set; }
}
My View is like this-
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Rating" class="control-label"></label>
<input asp-for="Rating" class="form-control" />
<span asp-validation-for="Rating" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="WatchDate" class="control-label"></label>
<input asp-for="WatchDate" readonly type="text" placeholder="Click to set start-date" class="DatePicker form-control" id="start_date">
<span asp-validation-for="WatchDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie" class="control-label"></label>
<select asp-for="Movie" asp-items="ViewBag.Movies" class="form-control">
<option disabled selected>-- Please Select a Movie to Review --</option>
</select>
<span asp-validation-for="Movie" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
And my Controller is like this-
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Rating,WatchDate,Movie")] Review review)
{
if (ModelState.IsValid)
{
review.Id = Guid.NewGuid();
_context.Add(review);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
//ViewBag.Movies and ViewData["Movies"] => Both are same
ViewData["Movies"] = new SelectList(await _context.Movies.ToListAsync(), "Id", "Name", review.Movie);
return View(review);
}
I am getting my view showing error message - 'Model is Invalid'.
can anyone please help?