简体   繁体   中英

How to move to another view from dropdown list in ASP.NET mvc

I'm stuck here for over a week. I'm trying to create a dropdown list where when a user clicks on certain value from the dropdown list, it will redirect to another view called index. So below is my dropdown code. Values in the dropdown list is obtained from SQL Server. What do I do, should I change in controller, or in the view?

@Html.DropDownListFor(model => model.Reference, ViewBag.ISharedUI as SelectList, "-- REFERENCE TYPE --")
@Html.ValidationMessageFor(x => x.reftab)

thanks in advance.

edit:

by the way, below is the code in my controller in order to show the values for the dropdown

   public ActionResult Create()
    {
        List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
        ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");
        return View("Create");
    }

    //Here here here here here here 
    // POST: ListOfItems/Create
    [HttpPost]
    public ActionResult Create(ListOfItems objListOfItems)
    {
        try
        {
            // TODO: Add insert logic here
            List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
            ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");


            ListOfItems objListOfItemss = new ListOfItems();
            objListOfItemss = ARSharedDAL.CreateARSharedInsert(objListOfItems);

            return RedirectToAction("Index");
        }
        catch
        {
            List<CommonEntities> ISharedUI = CommonDAL.GetARSharedReference();
            ViewBag.ISharedUI = new SelectList(ISharedUI, "ID", "Description");
            return View();
        }
    }

You can use jQuery. Give the drop down list an id, like so.

     <div id="FormDiv">
     @using (Html.BeginForm("Create", "CONTROLLER", null, FormMethod.Post, new { }))
          {
          @Html.DropDownListFor(model => model.Reference, ViewBag.ISharedUI as SelectList, "-- REFERENCE TYPE --",new {id = "dblist" })
          @Html.ValidationMessageFor(x => x.reftab)
          }
     </div>

Then use jQuery to submit its parent form

$('#dblist').on('change', function(event){
    var form = $(event.target).parents('form');
    form.submit();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM