0

我有一个下拉菜单需要触发带有已单击 id 值的 Url.Action

目前我有:模型

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; }
    }
}

看法

@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))

控制器方法

 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

下拉列表应根据全球价值列出一堆公司,选择一个后,它会建立关系。

我要完成的工作:根据从下拉列表中选择的内容触发 ActionResult SelectCompany。目前它似乎没有做任何事情,我应该在 VS 中使用任何好的调试工具来查看单击下拉菜单时会发生什么。从我的前端看来,当我选择一家公司时,好像什么都没有发生。

更新 我已经在我的视图中调用了脚本,但是我在使用其中的@url.Action 时遇到了问题,我必须通过这个:

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

从下拉列表中选择的值。

4

1 回答 1

1

您可以使用Change()下拉列表的 JQuery 事件。

实现这一目标的最简单方法是:

将以下代码放在您的视图中:

@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>
于 2013-10-03T18:05:42.797 回答