Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 11661

Selecting and getting city from a list and push them to database table in a column as a string

$
0
0

I have created a list of cities in my BranchService and I have called them in my create method in my controller. When I try to create a new Branch, I want to select a specific City from the dropdown (the dropdown is supposed to be filled with the list of the cities I have created in my service), and this selected city, will go to the 'City' column in 'Branches' table in database, where this column accepts string types. I mean, the city I will select in the dropdown, will fill the 'City' column in the database table as a string type.I have made something, but I think there is something wrong because when I try to create a new Branch and then click on the 'Create' button to submit my data, it doesn't happen anything.

this is my Branch Entity:

public class Branches: BaseEntity<int>{    public int Id { get; set; }    public string Name { get; set; }    public string Description { get; set; }    public string City { get; set; }}

branch model:

 public class BranchAddRequestModel : IValidatableObject    {        [Required(ErrorMessage = "Plotesoni emrin e deges!")]        [StringLength(50, MinimumLength = 2, ErrorMessage = "Gjate")]        [DisplayName("Emri")]        public string Name { get; set; }        [Required(ErrorMessage = "Ne")]        [DisplayName("Pershkrimi")]        [StringLength(255, ErrorMessage = "Persh!")]        public string Description { get; set; }        [Required(ErrorMessage = "Zgjid!")]        [DisplayName("Qyteti")]        public string City { get; set; }        public List<string> SelectCity { get; set; }

branch service:

 public class BranchService : IBranchesService {     private readonly IUnitOfWork _unitOfWork;     private readonly IServiceProvider _serviceProvider;     private readonly List<string>  _countries;     public BranchService(IServiceProvider serviceProvider)     {         _serviceProvider = serviceProvider;         _unitOfWork = _serviceProvider.GetService<IUnitOfWork>();         _countries = new List<string>     {"Berat", "Tirane"     };     }     public async Task Create(Branch branch)     {         var existsBranch = _unitOfWork.BranchRepository.GetByName(branch.Name);         if (existsBranch != null)         {             throw new Exception("Ekziston dega me kete emer!");         }        await _unitOfWork.BranchRepository.AddAsync(new DAL.Entities.Branches         {             Name = branch.Name,             Description = branch.Description,             City= branch.City         });        await _unitOfWork.CommitAsync();     }

branch controller:

public class BranchesController : Controller{    private IBranchesService _branchesService;    private readonly List<string> _cities;    public BranchesController(IBranchesService branchesService)    {        _branchesService = branchesService;        _cities= _branchesService.GetCountries() ?? new List<string>();    }    public IActionResult Index()    {        return View(_branchesService.GetBranches());    }    [HttpGet]    public IActionResult Create()    {        var model = new BranchAddRequestModel        {            SelectCity = _cities    };        //return View(new BranchAddRequestModel());        return View(model);    }    [HttpPost]    public IActionResult Create(BranchAddRequestModel model)    {        if (ModelState.IsValid)        {            _branchesService.Create(new Branch            {                Name = model.Name,                Description = model.Description,                City=model.City            });            return RedirectToAction("Index");        }         model.SelectCity = _branchesService.GetCountries() ?? new List<string>();        return View(model);    }

the part of view in create.cshtml

<label asp-for="City" class="control-label"></label><br /> <select asp-for="City" asp-items="new SelectList(Model.SelectCity)"><option value="">-- Zgjidh Qytetin --</option></select><span asp-validation-for="City" class="text-danger"></span>

Viewing all articles
Browse latest Browse all 11661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>