0

I have a drop down that needs to trigger a Url.Action with the id value that has been clicked

Currently I have: Model

using System.Collections.Generic;
using System.Data.Entity;
using System.Web.Mvc;

namespace Portal.Models
{
    public class CompanyViewModel
    {
        public int Id { get; set; }
        public string CompanyName { get; set; }
        public IEnumerable<SelectListItem> CompaniesList { get; set; }
    }
    public class CompanyViewModelDbContext : DbContext
    {
        public DbSet<CompanyViewModel> Contacts { get; set; }
    }
}

View

@model Portal.Models.CompanyViewModel

@{
    ViewBag.Title = "ShowDropdown";
}

@Html.DropDownListFor(m => m.Id, new SelectList(ViewBag.CompanyList as System.Collections.IEnumerable, "CoId","CompanyName"), Url.Action("SelectCompany", "Home", new { partnerCoId = "CoId" }, null))

Controller methods

 public PartialViewResult ShowDropdown()
        {
            var coid = Lt.GetThisUsersCoId();
            var model = new CompanyViewModel(); 
            using (var dc = new CompanyViewModelDbContext())
            {
                var content =
                    (from cr in db.CompanyRelationship
                        //This is grabbing all related companies to the logged in user
                        join c in db.Companies on cr.CoId equals c.CoId
                        where cr.PartnerCoId == coid
                        select new
                        {
                            cr.CoId,
                            c.CompanyName
                        }).Distinct().ToList();

                ViewBag.CompanyList = content;

                foreach (var item in content)
                {
                    model.Id = item.CoId;
                    model.CompanyName = item.CompanyName;
                }

            }

            return PartialView(model);
        }



public ActionResult SelectCompany(int partnerCoId, string redirect)
        {//Bunch of code that I didn't write but just needs to be triggered on click

The drop down should list a bunch of companies based on a global value, after one is select it sets up a relationship.

What I am trying to accomplish: Trigger the ActionResult SelectCompany based on what is chosen from the drop down. Currently it doesn't seem to do anything, is there any good debugging tools I should be using in VS to see what happens when I click the drop down. From my front end it appears as if nothing happens when I select a company.

UPDATE I have gotten the script call into my View but I am having trouble using the @url.Action from within it, I have to pass this:

@Url.Action("SelectCompany", "Home", new {partnerCoId = x})

The value selected from the drop down.

2
  • 1
    Are you open to JQuery? Commented Oct 3, 2013 at 17:50
  • I have zero experience with JQuery but I'm willing to learn, I thought this would be the simplest way to implement some one else's code. I am cleaning up multiple unnecessary calls that were being made, and trying to configure this drop down in as few lines of code as possible. Commented Oct 3, 2013 at 17:55

1 Answer 1

1

You can use JQuery Change() event of dropdownlist.

The Simplest Way to achieve this is :

Place the below code in your View:

@Html.DropDownListFor(m => Model.Id, new SelectList(ViewBag.CompanyList as System.Collections.IEnumerable,"Select", new { @id = "dropdwonId" })%>

<script>
    $("#dropdwonId").change(function() {       // This will fire when the dropdown value will get changed
         var url = "YourUrl/" + $(this).val(); //Append Id To URL
         window.location.replace(url);         
    });
</script>
Sign up to request clarification or add additional context in comments.

7 Comments

How do I link a dropdownId from an @html.DropDownList()? what I am doing with the Action is not changing the page, just changing what data is then displayed in a grid below based on a relationship. I am really not sure how this will function with what I am doing. Does this JQuery go in my View? Also, will I have to restructure all the work I have already done to implement this?
See the updated code. you can place jquery inside <script> block or you can place the jquery code in tour .js file.
Getting a "Cannot resolve id 'dropdownId'". This may be caused by the new { @id = "dropdwonId" } as it's giving me a string error.
Could this be caused by dropdownId, not being found in my model or ViewBag?
I am working with what you posted, thank you, and i'm trying to figure out how to get Url.Action("SelectCompany", "Home", new { partnerCoId = "CoId" } to function from within the JQuery
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.