Skip to content
Loading
Is it Possible to add two Get methods in 1 Action(asp.net core) ???
  • thank u sachin singh
  • Hamid Khan
    Dear Jon John,
     
    Yes of course it is possible.
    Please check it with routing
     
    [HttpGet("GetItem")] 

    [HttpGet("GetAllItem")]
     
  • Varun Setia
    Hi Jon,
     
    You must be passing data to View through ViewModel you can create 2 list inside ViewModel. Then Create an instance and pass data to View.
    1. public class MyViewModel{  
    2. public List<string> List1{get;set;}  
    3. public List<string> List2{get;set;}  
    4. }  
    To Call it in View
    1. public ActionResult MyMethod(){  
    2. MyViewModel vm = new MyViewModel();  
    3. vm.List1 = ..//Data Filling Logic  
    4. vm.List2 = ..//Data Filling Logic  
    5. return View(vm);  
    6. }  
    Else,
    You can pass data to View using ViewData or ViewBag
    1. public ActionResult MyMethod(){  
    2. ViewData['List1'] = ..//Data Filling logic  
    3. ViewData['List2'] = ..//Data Filling logic  
    4. return View();  
    5. }  
    6. public ActionResult MyMethod(){  
    7. ViewBag.List1 = ..//Data Filling logic  
    8. ViewBag.List2 = ..//Data Filling logic  
    9. return View();  
    10. }  
    View Code:
    1. @Html.DropDownList("MyList",new SelectList(Model.List1)) // Model.List1/ViewBag.List1/ViewData['List1']  
    2.   
    3. @foreach (var item in Model.List2) // Model.List2/ViewBag.List2/ViewData['List2']  
    4. {  
    5. @Html.CheckBox(item,false) @(" "+item) 
        
    6. }  
  • Khushbu Saini
    Okay you can bind data list in viewmodel in that list ...and then return it view you can see my this article this is of checkbox list add and edit example https://www.c-sharpcorner.com/article/multi-select-checkbox-dropdown-list-create-edit-in-net-core-3-1/
  • @khusbu Saini i get that but i want to know how we write code in get method of both checbox and dropdownlist in 1 action method..for eaxmple http[get] public iactionresult Index()-- in here
  • Khushbu Saini
    I think you can add both type lists as IDs array in Viewmodel and then bind those list to view with drodown and checkbox..so from this you can get both drop-down and checkbox list ids to your post method on controller.