displaying a different table based on the drop down selection

Anjali Agarwal 1,531 Reputation points
2023-10-14T12:29:47.8566667+00:00

I have the following drop down list on my page. I also have a onchange event if someone selects an item from the drop down list. I can see the item index in the onchange event:

@(Html.Kendo().DropDownList()
                .Name("Awards")
                .Label(label => label.Content("Please select Award type from the list. "))
                .DataTextField("AwardCategory1")
                .DataValueField("AwardId")
                .OptionLabel("Select award")
                
                .DataSource(source =>
                {
                    source.Read(read =>
                    {
                        read.Action("GetAwards", "Home");
                    });
                })
              .Events(e => e.Change("onChange"))
            )
			
			This is my onChange event:
			
			<script>
  function onChange(e) {
         var dropdownlist = $("#Awards").data("kendoDropDownList");
         var text = dropdownlist.text();
         $(".award").text(text);
         alert(text);
        
    }
</script>

In the alert, I can see the selected item from the dropdown. I want to display a table on my web page based on the selected item so if text =="Award5" then I want to display a totally different table on my Razor page. I have the following table on my page if "Award1", "Award2" and Award3" are selected from the drop down, but if "Award4" and "Arard5" is selected then I want to display a different table. Is it possible to do that:

display below table if users selects either "Award1", "Award2" and Award3" from drop down.

<table class="table  table-responsive table-hover btable">
                 
                    @foreach (var d in Model.Transactions)
                    {
       
                        <tr>
                            <td>@d.Status</td>
                            <td>@d.Type</td>
                            <td>@d.TransactionId</td>
                            <td> @d.Created.Value.ToShortDateString()</td>
                            <td>@d.StatusChangeDate.Value.ToShortDateString()</td>
                        </tr>
                    }
                </table>
				
		If the user selects "Award4" from the drop down then I want to select this table:

		

		
<table class="table  table-responsive table-hover btable">
                 
                    @foreach (var d in Model.Transactions)
                    {
       
                        <tr>
                            <td>@d.StatusName</td>
                            
                            <td>@d.TransactionName</td>
                            <td> @d.Created.Value.ToShortDateString()</td>
                            <td>@d.StatusChangeDate.Value.ToShortDateString()</td>
                        </tr>
                    }
                </table>
			This is the Razor page. Not sure, if it is possible with if else statement.

		

		
Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

Accepted answer
  1. Anonymous
    2023-10-16T09:43:54.67+00:00

    Hi,@Anjali Agarwal,you could send an ajax call as below to hit the hander in your page model and update the page on success

    <div id="mydiv">
    
    </div>
    
    <script>
        function onChange(e) {
            var dropdownlist = $("#Awards").data("kendoDropDownList");
            var text = dropdownlist.text();
            $(".award").text(text);
            alert(text);
            var url = "YourPage?handler=partial&text="+text ;
            $.ajax({
                url: url,
                method :"GET"  ,
                success:function(result){
                    $("#mydiv").html(result)
                }
            })
    
        }
    </script>
    
    
    

    Handler in your page model:

    public class YourPageModel : PageModel
        {
             //other handler 
             .......
           
            public IActionResult OnGetPartial(string text)
            {
                //pass the viewmodel contains the text to your razor view
                return Partial("Partial",yourviewmodel);
            }
        }
    

    Add a RazorView (notice not RazorPage)named Partial in your Pages folder

    User's image

    make the judegement in your RazorView(You could also make the judegment in the handler and return different partial view):

    @if(Model.SomeProperty...... )
    {
       .....
    }
    else
    {
        ........
    }  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.