I need to save string values from multiple check-boxes to a database via an API call. How do I properly model this in my database model such that I can save the string values into one column in my database table. I'm using ASP.NET MVC, I used vue.js on the front-end.
I want to pass an array of strings that represent values from multiple check-boxes to an API endpoint. I represent this as a list of strings in my database model and also want to save the list of string values as a string in my table column.(Note: Not sure if this is a good approach as i'll have to able to retrieve this list later from the database).
public class TempAccountOpeningRequest
{
[Key]
public int Id { get; set; }
[Required(ErrorMessage = "First Name is required")]
[JsonProperty("first_name")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
[JsonProperty("last_name")]
public string LastName { get; set; }
[JsonProperty("typesSelected")]
public List<string> TypesSelected { get; set; }
}
I want to be able to pass the values from the multiple checkboxes to an API and then API saves the data to a my data base table
You can save it in one column with comma separate:
Change your Model to:
public class TempAccountOpeningRequest
{
...
[JsonProperty("typesSelected")]
public string TypesSelected { get; set; }
}
Your SaveDTO can be like this: ( TypesSelected
will send from vue.js as an array)
public class SaveTempAccountOpeningRequestDTO
{
public string FirstName { get; set; }
public string LastName { get; set; }
public List<string> TypesSelected { get; set; }
}
And your API get data and then convert it to an array and save it in the database:
public bool Save(SaveTempAccountOpeningRequestDTO dto)
{
...
string joinedTypesSelected = string.Join(',', dto.TypesSelected);
...
}
Of course, when you get your data, you should change it to list like this:
yourResult.Select(x=>x.TypesSelected.Split(',')).ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.