Sourabh Dhiman

Sourabh Dhiman

  • 1.6k
  • 391
  • 14.8k

Model validation message in mvc.

Sep 15 2020 12:06 PM
I have face a problem in Model Validation .It's me model code 
public class EducationalImageViewModel
{
[Required(ErrorMessage = "Please enter ImageTitle")]
public string Title { get; set; }
//public string Video { get; set; }
[Required(ErrorMessage = "File Shouldn't Be Empty Please select file")]
public string Image { get; set; }
public int EducationalId { get; set; }
[Required(ErrorMessage = "Please enter ImageDescription")]
public string Description { get; set; }
}
public class EducationalImageList
{
public List<EducationalImageViewModel> lst_EducationImageData { get; set; }
[Required(ErrorMessage = "Please enter ImageTitle")]
public string Title { get; set; }
//public string Video { get; set; }
[Required(ErrorMessage = "File Shouldn't Be Empty Please select file")]
public string Image { get; set; }
public int EducationalId { get; set; }
[Required(ErrorMessage = "Please enter ImageDescription")]
public string Description { get; set; }
}
This is my view code 
<div class="modal fade" id="add-category" tabindex="-1" role="dialog" aria-labelledby="add-category">
<div class="modal-dialog modal-dialog-centered" role="document">
@using (Html.BeginForm("EducationalI", "EducationalManagement", FormMethod.Post, new { id = "CreateForm", enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<form>
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title has-icon ms-icon-round "> Add New Image</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
</div>
<div class="modal-body">
<div class="ms-form-group">
<label for="img">Select image: </label>&nbsp
<input type="file" id="img" name="EducationImage" accept="image/*">
@*@Html.TextBoxFor(x => x.Image, new { @type = "text", @id = "img", @class = "form-control" })*@
@Html.ValidationMessageFor(x => x.Image, "", new { @class = "text-danger" })
</div>
<div class="ms-form-group has-icon">
<label for="choosecategory">Image Title</label>
@Html.TextBoxFor(x => x.Title, new { @type = "text", @id = "ImageName", @class = "form-control", @placeholder = "Enter Title" })
@*<input type="text" class="form-control" placeholder="Enter your Category">*@
@Html.ValidationMessageFor(x => x.Title, "", new { @class = "text-danger" })
</div>
<div class="ms-form-group has-icon">
<label for="choosecategory">Image Description</label>
@Html.TextBoxFor(x => x.Description, new { @type = "text", @id = "DescriptionName", @class = "form-control", @placeholder = "Enter Description", })
@*<input type="text" class="form-control" placeholder="Enter your Category">*@
@Html.ValidationMessageFor(x => x.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-info shadow-none" id="SubmitImage">Submit</button>
</div>
</div>
</form>
}
Validation message are not show after click button. 
 
 

Brought to you by:

Answers (2)