Reputation: 7
This is in ASP.NET Core MVC 2.1.
So what I want to do is validate a list of objects I read from a file.
The controller successfully reads the file and gets the data. I am using a DTO version of the model, heard it is better to do like that.
The main entity model.
public class Person
{
[Key]
public int ID { get; set; }
[Required(ErrorMessage = "Osoba obavezno mora imati ime")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Osoba obavezno mora imati prezime")]
public string LastName { get; set; }
[Required(ErrorMessage = "Grad obavezno mora imati ime")]
public string CityName { get; set; }
[Required(ErrorMessage = "Osoba obavezno mora imati postanski broj")]
[StringLength(5, MinimumLength = 5, ErrorMessage = "Poštanski broja mora sadrži točno 5 znamenki.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Poštanski broj smije sadržavati samo znamenke.")]
public string PostalCode { get; set; }
[Required(ErrorMessage = "Osoba obavezno mora imati mobilni broj")]
[StringLength(11, MinimumLength = 11, ErrorMessage = "Mobilni broja mora sadrži točno 11 znamenki.")]
[RegularExpression("^[0-9]*$", ErrorMessage = "Mobilni broj smije sadržavati samo znamenke.")]
public string MobileNumber { get; set; }
public string FullName => $"{FirstName} {LastName}";
}
The DTO entity model.
public class PersonModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string CityName { get; set; }
public string PostalCode { get; set; }
public string MobileNumber { get; set; }
}
The method used in the controller.
public IActionResult LoadData()
{
PeopleService peopleService = new PeopleService();
var path = "pathToFile";
var resultData = peopleService.ReadCSVFile(path);
return PartialView("_LoadDataTable", resultData);
}
So now my question is what is the best way to validate the data from the file?
I experimented using the entity model it worked, but I seems like a bad idea because you have to call the database every time. I want to validate it client side first I guess. Also, I need to get the error message back so that I can display it on view.
Upvotes: 0
Views: 894
Reputation: 5041
You can use TryValidateModel
to valid the object from file. Document
You need to convert the string to a specific object fisrt, then call TryValidateModel
.
public IActionResult LoadData()
{
//read the content from file and convert it to Person
string readText = "";
FileStream fileStream = new FileStream(@"path", FileMode.Open);
using (StreamReader reader = new StreamReader(fileStream))
{
readText += reader.ReadToEnd();
}
var person = JsonConvert.DeserializeObject<Person>(readText);
var valid=TryValidateModel(person);
List<string> listError = new List<string>();
foreach(var value in ModelState.Values)
{
if (value.Errors.Count > 0)
listError.Add(value.Errors[0].ErrorMessage);
}
ViewBag.errors = listError;
//...
}
View
@foreach(var e in (List<string>)ViewBag.errors)
{
<span class="text-danger">@e</span>
}
Another way is to use ValidationSummary
in the view.
@Html.ValidationSummary()
Upvotes: 2