locked
dropdownlist mvc RRS feed

  • Question

  • User-265772647 posted

    Hi,

    I have created dropdownlist ,depending  on selected option r and click on submit button it should redirect to different page. here I have written but not able to work.

    Belowis the action method in Controller:

    [HttpPost]

    public ActionResult UserScreen()

    {

    ViewBag.BranchOptions = AdminScreen.GetBranchOffice();//getting options from model into viewbag

    if (ViewBag.BranchOptions.SelectedList("T Officer"))// here getting error,could  I get any help how to call dropdownlist option ,I have only two options in dropdown

    { return View("ClientProfScr");//CProfScr.html

    }

    else

    {

    return View("ClientPSearch");//Cprofilesearch.html } }

    I this is UserView: @model Maintenance.Models.AdminScreen

    @{ Layout = null; }

    <div id="login-div">@using (Html.BeginForm("User", "UserAccess", FormMethod.Post)) {

    Admin Login
    <label>Entitlement</label> <div>@Html.DropDownListFor(model => model.Entitlement, ViewBag.EntitlementOptions as List, "--Select Entitlement--") </div>
    <input type="submit" value="Login" />

    }</div> <div></div>

    Here is AdminScreen Model:

    public class adminScreen()

    {

    public string Entitlement { get; set; }

    public static IEnumerable GetEntitlement()

    { List EntitlementOptions = new List()

    { new System.Web.Mvc.SelectListItem()

    {Text="T Officer" , Value = "1"}, new System.Web.Mvc.SelectListItem() {Text="Customer Officer" , Value = "2"}, };

    return EntitlementOptions; }

    }

    Monday, April 27, 2020 8:16 PM

Answers

  • User-1330468790 posted

    Hi emaal,

     

    I think you have been confuse by how to pass value between page and code behind controller.

    emaak

    ViewBag.BranchOptions = AdminScreen.GetBranchOffice();//getting options from model into viewbag

    ViewBag only transfers data from controller to view, not visa-versa. ViewBag values will be null if redirection occurs.

    If you select a value from the DropDownListFor, you could not get the selected value from ViewBag.BranchOptions.

    PS: The names in your codes are not matching with each other. You have to make them consistent in the project.

    For example, ViewBag.BranchOptions vs ViewBag.EntitlementOptions,  GetEntitlement() vs GetBranchOffice().

    I think the members/methods on both sides should be the same.

       

    emaak

    depending  on selected option r and click on submit button it should redirect to different page. here I have written but not able to work.

    I assume that you have created two different views "ClientProfScr" and "ClientPSearch". The only thing the controller have to do is to identify the selected value and redirect to/return view which matches with the selected value.

     

    You could refer to below demo to see if it does help.

     

    Index cshtml.

    @model MVCCaseDemo.Models.AdminScreen
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    <div id="login-div">
        @using (Html.BeginForm("User", "UserAccess", FormMethod.Post))
        {
        <table>
            <tr>
                <td>
                    Admin Login
                </td>
                <td>
                    Entitlement
                </td>
            </tr>
            <tr>
                <td>
                    <div>@Html.DropDownListFor(model => model.Entitlement, (List<SelectListItem>)ViewBag.EntitlementOptions, "--Select Entitlement--") </div>
                </td>
                <td>
                    <input type="submit" value="Login" />
                </td>
            </tr>
        </table>
        }
    </div>
    

    AdminScreen.cs

     public class AdminScreen
    
        {
    
            public string Entitlement { get; set; }
    
    
            public static IEnumerable GetEntitlement()
    
            {
                List<SelectListItem> EntitlementOptions = new List<SelectListItem>()
    
                { 
                    new System.Web.Mvc.SelectListItem()
                    {
                        Text="T Officer", 
                        Value = "1"
                    },
                    new System.Web.Mvc.SelectListItem() 
                    {
                        Text="Customer Officer",
                        Value = "2"
                    },
                };
    
                return EntitlementOptions;
            }
    
        }

    UserAccessController.cs

       public class UserAccessController : Controller
        {
            // GET: UserAccess
            public ActionResult Index()
            {
                ViewBag.EntitlementOptions = AdminScreen.GetEntitlement();
                return View();
            }
    
            [HttpPost]
            public ActionResult User(AdminScreen admin)
    
            {
                
                if (admin.Entitlement.Equals("1"))// Here you will get the selected value, not selected text.
    
                {
                    return View("ClientProfScr");//CProfScr.cshtml
    
                }
    
                else 
    
                {
    
                    return View("ClientPSearch");//Cprofilesearch.cshtml 
                }
            }
    
            // GET: ClientPSearch
            public ActionResult ClientPSearch()
            {
                return View("ClientPSearch");
            }
    
            // GET: ClientProfScr
            public ActionResult ClientProfScr()
            {
                return View("ClientProfScr");
            }
        }

    ClientProfScr.cshtml

    @{
        ViewBag.Title = "ClientProfScr";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>ClientProfScr</h2>
    
    This is
    <h3>ClientProfScr</h3>
    Page.
    
    

    ClientPSearch.cshtml 

    @{
        ViewBag.Title = "ClientPSearch";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>ClientPSearch</h2>
    
    This is 
    <h3>ClientPSearch</h3> 
    Page.
    

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 2:50 AM

All replies

  • User475983607 posted

    emaak

    I have created dropdownlist ,dependog on selected option redirect to different view

    You misunderstand MVC.  Redirects target actions not Views.  

    Secondly, you have not explained what's not working nor have you shared enough code to reproduce this issue.   The community needs enough code to reproduce problem otherwise it is very difficult to help. 

    IMHO, the code that you have shown is not well written and you should use standard MVC programming patterns found in any beginning level tutorial.

    Monday, April 27, 2020 8:31 PM
  • User-1330468790 posted

    Hi emaal,

     

    I think you have been confuse by how to pass value between page and code behind controller.

    emaak

    ViewBag.BranchOptions = AdminScreen.GetBranchOffice();//getting options from model into viewbag

    ViewBag only transfers data from controller to view, not visa-versa. ViewBag values will be null if redirection occurs.

    If you select a value from the DropDownListFor, you could not get the selected value from ViewBag.BranchOptions.

    PS: The names in your codes are not matching with each other. You have to make them consistent in the project.

    For example, ViewBag.BranchOptions vs ViewBag.EntitlementOptions,  GetEntitlement() vs GetBranchOffice().

    I think the members/methods on both sides should be the same.

       

    emaak

    depending  on selected option r and click on submit button it should redirect to different page. here I have written but not able to work.

    I assume that you have created two different views "ClientProfScr" and "ClientPSearch". The only thing the controller have to do is to identify the selected value and redirect to/return view which matches with the selected value.

     

    You could refer to below demo to see if it does help.

     

    Index cshtml.

    @model MVCCaseDemo.Models.AdminScreen
    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>Index</h2>
    
    <div id="login-div">
        @using (Html.BeginForm("User", "UserAccess", FormMethod.Post))
        {
        <table>
            <tr>
                <td>
                    Admin Login
                </td>
                <td>
                    Entitlement
                </td>
            </tr>
            <tr>
                <td>
                    <div>@Html.DropDownListFor(model => model.Entitlement, (List<SelectListItem>)ViewBag.EntitlementOptions, "--Select Entitlement--") </div>
                </td>
                <td>
                    <input type="submit" value="Login" />
                </td>
            </tr>
        </table>
        }
    </div>
    

    AdminScreen.cs

     public class AdminScreen
    
        {
    
            public string Entitlement { get; set; }
    
    
            public static IEnumerable GetEntitlement()
    
            {
                List<SelectListItem> EntitlementOptions = new List<SelectListItem>()
    
                { 
                    new System.Web.Mvc.SelectListItem()
                    {
                        Text="T Officer", 
                        Value = "1"
                    },
                    new System.Web.Mvc.SelectListItem() 
                    {
                        Text="Customer Officer",
                        Value = "2"
                    },
                };
    
                return EntitlementOptions;
            }
    
        }

    UserAccessController.cs

       public class UserAccessController : Controller
        {
            // GET: UserAccess
            public ActionResult Index()
            {
                ViewBag.EntitlementOptions = AdminScreen.GetEntitlement();
                return View();
            }
    
            [HttpPost]
            public ActionResult User(AdminScreen admin)
    
            {
                
                if (admin.Entitlement.Equals("1"))// Here you will get the selected value, not selected text.
    
                {
                    return View("ClientProfScr");//CProfScr.cshtml
    
                }
    
                else 
    
                {
    
                    return View("ClientPSearch");//Cprofilesearch.cshtml 
                }
            }
    
            // GET: ClientPSearch
            public ActionResult ClientPSearch()
            {
                return View("ClientPSearch");
            }
    
            // GET: ClientProfScr
            public ActionResult ClientProfScr()
            {
                return View("ClientProfScr");
            }
        }

    ClientProfScr.cshtml

    @{
        ViewBag.Title = "ClientProfScr";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>ClientProfScr</h2>
    
    This is
    <h3>ClientProfScr</h3>
    Page.
    
    

    ClientPSearch.cshtml 

    @{
        ViewBag.Title = "ClientPSearch";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>ClientPSearch</h2>
    
    This is 
    <h3>ClientPSearch</h3> 
    Page.
    

    Demo:

     

    Hope this can help you.

    Best regards,

    Sean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 28, 2020 2:50 AM
  • User-265772647 posted

    Thank you Sean. Its working now.

    Tuesday, April 28, 2020 3:17 PM