skip to Main Content

I’m working on an ASP.NET MVC application and I’m looking to enhance the user experience by adding image upload functionality to store images in my database table. Additionally, I want to incorporate dropdown menus populated from other tables and checkboxes for user selections. How can I achieve this in ASP.NET MVC? I’m particularly interested in the steps required for setting up the model, view, and controller to handle image uploads, dropdowns from other tables, and checkboxes effectively. Any insights or code examples would be greatly appreciated

2

Answers


  1.     @model tblCrud
        
        @{
            ViewData["Title"] = "Insert";
        }
        
        <h2>Add Data</h2>
        
        <link href="https://kendo.cdn.telerik.com/2022.1.301/styles/kendo.default-v2.min.css" rel="stylesheet">
        <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
        <script src="https://kendo.cdn.telerik.com/2022.1.301/js/kendo.all.min.js"></script>
        
        <form asp-action="Add" method="post" enctype="multipart/form-data">
            <div class="container mt-5">
                <div class="form-group">
                    <label for="c_name" class="form-label">Name:</label>
                    <input name="c_name" id="c_name" asp-for="c_name" class="kendo-textbox" />
                </div>
        
                <div class="form-group">
                    <label class="form-label">Gender:</label>
                    <input type="radio" name="c_gender" id="c_male" value="Male" />
                    <label for="c_male">Male</label>
                    <input type="radio" name="c_gender" id="c_female" value="Female" />
                    <label for="c_female">Female</label>
                    <input type="radio" name="c_gender" id="c_other" value="Other" />
                    <label for="c_other">Other</label>
                </div>
        
                <div class="form-group">
                    <label class="form-label">Image</label>
                    <input type="file" name="c_image" id="c_image" />
                </div>
        
                <div class="form-group">
                    <label for="c_date" class="form-label">Date:</label>
                    <input name="c_date" id="c_date" asp-for="c_date" class="kendo-datepicker" />
                </div>
        
                <div class="form-group">
                    <label for="c_city" class="form-label">City:</label>
                    <input name="c_city" id="c_city" asp-for="c_city" class="kendo-dropdownlist" />
                </div>
        <br>
        <br>
                <div class="form-group">
                    <input type="checkbox" name="c_skill" value="Design" id="checkbox1" />
                    <label for="checkbox1">Design</label>
                    <input type="checkbox" name="c_skill" value="Develop" id="checkbox2" />
                    <label for="checkbox2">Develop</label>
                    <input type="checkbox" name="c_skill" value="Testing" id="checkbox3" />
                    <label for="checkbox3">Testing</label>
                </div>
            </div>
            <input type="submit" value="Add" class="btn btn-primary">
        </form>
        
        <script>
            $(document).ready(function(){
        
                $("#c_name").kendoTextBox();
                $("input[name='c_gender']").kendoRadioButton();
                $("#c_image").kendoUpload();
                $("#c_date").kendoDatePicker();
                $("#c_city").kendoDropDownList({
                    autoBind:false,
                    optionLabel:"Select City",
                    dataTextField:"c_cname",
                    dataValueField:"c_id",
                    dataSource:{
                        type:"json",
                        transport:{
                            read:"/Crud/GetAllCities"
                        }
                    }
                });
            });
        </script>
    
    
    @model tblCrud
    
    @{
        ViewData["Title"] = "Edit";
    }
    
    <h2>Edit Data</h2>
    
    <link href="https://kendo.cdn.telerik.com/2022.1.301/styles/kendo.default-v2.min.css" rel="stylesheet">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2022.1.301/js/kendo.all.min.js"></script>
    
    <form asp-action="Edit" method="post" enctype="multipart/form-data">
    
        <div >
            <input type="hidden" asp-for="@Model.c_id">
        </div>
        <div class="container mt-5">
            <div class="form-group">
                <label for="c_name" class="form-label">Name:</label>
                <input name="c_name" id="c_name" asp-for="c_name" class="kendo-textbox" />
            </div>
    
            <div class="form-group">
                <label class="form-label">Gender:</label>
                <input type="radio" name="c_gender" id="c_male" value="Male" @(Model.c_gender == "Male" ? "checked" : "") />
                <label for="c_male">Male</label>
                <input type="radio" name="c_gender" id="c_female" value="Female" @(Model.c_gender == "Female" ? "checked" : "") />
                <label for="c_female">Female</label>
                <input type="radio" name="c_gender" id="c_other" value="Other" @(Model.c_gender == "Other" ? "checked" : "") />
                <label for="c_other">Other</label>
            </div>
    
            <div class="form-group">
                <label for="c_image" class="form-label">Image:</label>
                <input type="file" name="c_image" id="c_image" class="kendo-upload" />
                <img src="@Model.c_image" style="height: 150px; width:150px" />
            </div>
    
            <div class="form-group">
                <label for="c_date" class="form-label">Date:</label>
                <input name="c_date" id="c_date" asp-for="c_date" class="kendo-datepicker" />
            </div>
    
            <div class="form-group">
                <label for="c_city" class="form-label">City:</label>
                <input name="c_city" id="c_city" asp-for="c_city" class="kendo-dropdownlist" />
            </div>
    
    
            <div class="form-group">
                <input type="checkbox" name="c_skill" value="Design" @(Model.c_skill.Contains("Design")? "checked":"") />
                <label for="checkbox1">Design</label>
                <input type="checkbox" name="c_skill"  value="Develop" @(Model.c_skill.Contains("Develop")? "checked":"") />
                <label for="checkbox2">Develop</label>
                <input type="checkbox" name="c_skill" value="Testing" @(Model.c_skill.Contains("Testing")? "checked":"") />
                <label for="checkbox3">Testing</label>
            </div>
        </div>
        <input type="submit" value ="Edit" class="btn btn-primary">
    </form>
    
    <script>
        $(document).ready(function(){
            $("#c_name").kendoTextBox();
            $("input[name='c_gender']").kendoRadioButton();
            $("#c_image").kendoUpload();
            $("#c_date").kendoDatePicker();
            $("#c_city").kendoDropDownList({
                autoBind:false,
                optionLabel:"Select City",
                dataTextField:"c_cname",
                dataValueField:"c_id",
                dataSource:{
                    type:"json",
                    transport:{
                        read:"/Crud/GetAllCities"
                    }
                }
            });
        });
    </script>
    
    Login or Signup to reply.
  2. repository 
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Kendo_Grid.Models;
    using Npgsql;
    
    namespace Kendo_Grid.Repositories
    {
        public class CrudRepository : CommonRepositories, ICrudRepository
        {
            public void Add(tblCrud person, IFormFile c_image)
            {
                try{
                    _conn.Open();
                    if(c_image !=null && c_image.Length>0){
                        var filename = Path.GetFileName(c_image.FileName);
                        var fileextension = Path.GetExtension(c_image.FileName);
                        var Uniquename= Guid.NewGuid().ToString() + fileextension;
                        var savepath = Path.Combine(Directory.GetCurrentDirectory(), "Wwwroot/Images", Uniquename);
                        using (var Stream =new  FileStream(savepath, FileMode.Create)){
                            c_image.CopyTo(Stream);
                        }
                        person.c_image = "/Images/"+ Uniquename;
    
                    }
    
                    using (var cmd = new NpgsqlCommand("INSERT INTO public.t_practice05( c_name, c_image, c_gender, c_skill, c_date, c_cityid) VALUES ( @c_name,@c_image , @c_gender, @c_skill, @c_date, @c_city);", _conn)){
                        cmd.Parameters.AddWithValue("@c_name", person.c_name);
                        cmd.Parameters.AddWithValue("@c_image", person.c_image);
                        cmd.Parameters.AddWithValue("@c_date", person.c_date);
                        cmd.Parameters.AddWithValue("@c_skill", person.c_skill);
                        cmd.Parameters.AddWithValue("@c_city", person.c_cityid);
                        cmd.Parameters.AddWithValue("@c_gender", person.c_gender);
                        cmd.ExecuteNonQuery();
                    }
    
                }
                catch(Exception Ex){
                    Console.WriteLine("error at Add--->"+Ex.Message);
                }
                finally{
                    _conn.Close();
                }
            }
    
            public void Delete(int id)
            {
                // throw new NotImplementedException();
                try{
                    _conn.Open();
                    using (var cmd = new NpgsqlCommand("DELETE FROM public.t_practice05 WHERE c_id = @c_id;", _conn)){
                        cmd.Parameters.AddWithValue("@c_id", id);
                        cmd.ExecuteNonQuery();
                    }
    
                }
                catch(Exception Ex){
                    Console.WriteLine("error at Delete--->"+Ex.Message);
                }
                finally{
                    _conn.Close();
                }
            }
    
            public List<tblCrud> Getall()
            {
                // throw new NotImplementedException();
                var persons = new List<tblCrud>();
                try{
                    _conn.Open();
                    //SELECT p.c_id, p.c_name, p.c_image, p.c_gender, p.c_skill, p.c_date, p.c_city , c.c_name FROM t_practice5 p Inner Jion t_city c Where p.c_ = c.c_id;
                    using (var cmd = new NpgsqlCommand("SELECT c_id, c_name, c_image, c_gender, c_skill, c_date, c_cityid FROM public.t_practice05;", _conn)){
                        var reader = cmd.ExecuteReader();
    
                        while(reader.Read()){
                            var person = new tblCrud{
                                c_id = Convert.ToInt32(reader["c_id"]),
                                c_name = reader["c_name"].ToString(),
                                c_cityid = Convert.ToInt32(reader["c_cityid"]),
                                c_skill = (string[])reader["c_skill"],
                                c_date = reader["c_date"].ToString(),
                                c_gender = reader["c_gender"].ToString(),
                                c_image = reader["c_image"].ToString(),
                            };
                            persons.Add(person);
                        }
                    }
    
                }
                catch(Exception Ex){
                    Console.WriteLine("error at GetAll--->"+Ex.Message);
                }
                finally{
                    _conn.Close();
                }
                return persons;
            }
    
            public tblCrud GetById(int id)
            {
                // throw new NotImplementedException();
                var person = new tblCrud();
                try{
                    _conn.Open();
                using (var cmd = new NpgsqlCommand("SELECT c_id, c_name, c_image, c_gender, c_skill, c_date, c_cityid FROM public.t_practice05 WHERE c_id = @c_id;", _conn)){
                    cmd.Parameters.AddWithValue("@c_id", id);
                    var reader =  cmd.ExecuteReader();
                    if(reader.Read()){
                        person.c_id = Convert.ToInt32(reader["c_id"]);
                        person.c_name = reader["c_name"].ToString();
                        person.c_date = reader["c_date"].ToString();
                        person.c_image = reader["c_image"].ToString();
                        person.c_cityid = Convert.ToInt32(reader["c_cityid"]);
                        person.c_gender = reader["c_gender"].ToString();
                        person.c_skill = (string[])reader["c_skill"];
                    }
                }   
                }
                catch(Exception Ex){
                    Console.WriteLine("error at GetById--->"+Ex.Message);
                }
                finally{
                    _conn.Close();
                }
                return person;
            }
    
            
    
            public void Update(tblCrud person, IFormFile c_image)
            {
                // throw new NotImplementedException();
                try{
                    _conn.Open();
                    if(c_image!=null && c_image.Length>0){
    
                    var filename = Path.GetFileName(c_image.FileName);
                    var fileextension= Path.GetExtension(c_image.FileName);
                    var Uniquename = Guid.NewGuid().ToString()+ fileextension;
                    var savepath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/Images", Uniquename);
                    using (var Stream = new FileStream(savepath,FileMode.Create)){
                        c_image.CopyTo(Stream);
                    }
                    person.c_image= "/Images/"+Uniquename;
    
                    }
                    else{
                        using(var cmd = new NpgsqlCommand(" SELECT c_image FROM public.t_practice05 WHERE c_id = @c_id" , _conn)){
                            cmd.Parameters.AddWithValue("@c_id",person.c_id );
                            var images = cmd.ExecuteScalar().ToString();
                            person.c_image = images;
                        }
                    }
                    using(var cmd = new NpgsqlCommand("UPDATE public.t_practice05 SET  c_name=@c_name, c_image=@c_image, c_gender=@c_gender, c_skill=@c_skill, c_date=@c_date, c_cityid=@c_city WHERE c_id = @c_id;", _conn)){
                       
                        cmd.Parameters.AddWithValue("@c_id", person.c_id);
                        cmd.Parameters.AddWithValue("@c_name", person.c_name);
                        cmd.Parameters.AddWithValue("@c_image", person.c_image);
                        cmd.Parameters.AddWithValue("@c_date", person.c_date);
                        cmd.Parameters.AddWithValue("@c_skill", person.c_skill);
                        cmd.Parameters.AddWithValue("@c_city", person.c_cityid);
                        cmd.Parameters.AddWithValue("@c_gender", person.c_gender);
                        cmd.ExecuteNonQuery();
                    }
                }
                catch(Exception Ex){
                    Console.WriteLine("error at Update--->"+Ex.Message);
                }
                finally{
                    _conn.Close();
                }
            }
    
            public List<tblCity> GetAllCity(){
                var cities =  new List<tblCity>();
                try{
                    _conn.Open();
                    using(var cmd = new NpgsqlCommand("SELECT c_id, c_name FROM public.t_city;", _conn)){
                    var reader = cmd.ExecuteReader();
                    while(reader.Read()){
                        var city = new tblCity{
                            c_cid = Convert.ToInt32(reader["c_id"]),
                            c_cname = reader["c_name"].ToString(),
                        };
                        cities.Add(city);
                     }
                    }
                }
                catch(Exception Ex){
                    Console.WriteLine("Error at Get all Cities"+Ex.Message);
                }
                finally{
                    _conn.Close();
    
                }
                return cities;
            }
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search