I have a viewModel for the Index view. The problem is that when remote attribute is trying to do it's job the IsUnique method in my controller receives name = null, though i expext it to be a value that I put inside input field.
It works with another view, model of which is just SetViewModel. And here i have this vm inside another vm. On the index page I have sets in total as well as a button to create a new one. So I decided to create a vm, that contains everything for the view.
What am I doing wrong in this scenario? Why is it null? When I fill the input field and submit the form it shouldn't be null. In the POST method it comes with a name, but not here.
It works with another view, model of which is just SetViewModel. And here i have this vm inside another vm. On the index page I have sets in total as well as a button to create a new one. So I decided to create a vm, that contains everything for the view.
This is a viewModel for the index view. Here I want you to notice the SetViewModel vm, that contains validation attributes (Remote as well):
public class IndexViewModel
{
public IEnumerable<SetViewModel> Sets { get; set; }
public SetViewModel NewSet { get; set; }
public IndexViewModel()
{
Sets = [];
NewSet = new();
}
}
SetViewModel itself with the property Name:
public class SetViewModel
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is required.")]
[Display(Name = "Name of the set")]
[MaxLength(2, ErrorMessage = "The name of the set must be with a maximum length of {0} characters")]
[Remote(action: "CheckSet", controller: "Home", areaName: "Sets", HttpMethod = "POST", AdditionalFields = "Id", ErrorMessage = "Set with this name already exists")]
public string Name { get; set; } = null!;
}
Method IsUnique in a cotroller. The parameter name is null from index view, but it's working in another view with just SetViewModel as a model.:
[HttpPost]
public async Task<IActionResult> CheckSet(string name, int id)
{
return Json(!await IsSetUnique(name, id));
}
What am I doing wrong in this scenario? Why is it null? When I fill the input field and submit the form it shouldn't be null. In the POST method it comes with a name, but not here.