使用下拉列表选定索引中的相关数据填充文本框

时间:2015-01-05 20:19:02

标签: c# jquery json asp.net-mvc drop-down-menu

我有一个工作级联下拉列表,一旦我选择一个值,它就会使用JSON填充第二个下拉列表。我不知道如何将第二个下拉列表索引(以及每次更改时)传递给我的控制器以显示我模型中的相应数据。

我的json下拉列表如下

$(function () { 
    $('#VoyageDivID').hide(); 
    $('#tabsDiv').hide();

    $('#Vessel_ID').change(function () { 
        var URL = $('#VesselVoyageFormID').data('voyagelistaction')+'/' + $('#Vessel_ID').val(); 

        $.getJSON(URL, function (data) { 
            var items = ""; 
            $.each(data, function (i, voyage) { 
                items += "<option value='" + voyage.Value + "'>" + voyage.Text + "</option>"; 
            }); 

            $('#Voyage_ID').html(items); 
            $('#VoyageDivID').show(); 
            $('#tabsDiv').show();

        }); 
    }); 

    $('#Voyage_ID').change(function () { 
        $('#tabsDiv').show();
        //need to show the related data here whenever the dropdownlist is changed
    }); 
});

查看

<div id="tabsDiv">

<div class="editor-label">
      XYZ
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.XYZ)
    </div>        
</div> 

CONTROLLER

public ActionResult VoyageList(int ID)
{
    // int voyageID = ID;
    var voyages = from s in db.EXAMPLE.OrderByDescending(i => i.Voyage_ID).ToList()
                  where s.Vessel_ID == ID
                  select s;

    if (HttpContext.Request.IsAjaxRequest())//only if it comes from list
        return Json(new SelectList(
                        voyages.ToArray(),
                        "Voyage_ID",
                        "Voyage_ID")
                   , JsonRequestBehavior.AllowGet);

    return RedirectToAction("Index");
} 

0 个答案:

没有答案