简体   繁体   English

局部视图无法正确呈现

[英]Partial view is not rendering properly

I have two drop-downs when we are selecting the first one we need to update the second one. 在选择第一个下拉菜单时,我有两个下拉菜单需要更新第二个下拉菜单。
Below is my view: 以下是我的看法:

@model HostingManager.Models.ContractManager
@{
    ViewBag.Title = "CustomCreate"; 
}
@Html.Partial("_GroupDDL" , Model)
@Html.Partial("SelectClient", Model)
@Html.Partial("SelectContract", Model)

Below is first Drop-down (Partial View: _GroupDDL ) 以下是第一个下拉_GroupDDL (局部视图: _GroupDDL

@model HostingManager.Models.ContractManager

@using (Ajax.BeginForm("SelectClient", "Contracts", new AjaxOptions { UpdateTargetId = "IClients" }))
{ 
    @Html.DropDownListFor(
        m => m.SelectedGroupId,
        new SelectList(Model.IGroups, "id", "name"),
        string.Empty
    )
}
<script type="text/javascript">
    $('#SelectedGroupId').change(function () {
        $(this).parents('form').submit();
    });
</script> 

Below is Second Drop-down (Partial View: SelectClient ) 以下是第二个下拉SelectClient (局部视图: SelectClient

@model HostingManager.Models.ContractManager

@if (Model.IClients != null && Model.IClients.Count() > 0)
{
    using (Ajax.BeginForm("SelectContracts", "Contracts", new AjaxOptions { UpdateTargetId = "IContracts" }))
    { 
        @Html.HiddenFor(m => m.SelectedGroupId)
        @Html.DropDownListFor(
            m => m.SelectedClientId,
            new SelectList(Model.IClients, "id", "cname"),
            string.Empty
        )
    }
}

<script type="text/javascript">
    $('#SelectedClientId').change(function () {
        $(this).parents('form').submit();
    });
</script>

Below is my Controller action: 以下是我的控制器动作:

public ActionResult CustomCreate()
{
    ContractManager CM = new ContractManager();
    CM.IGroups = db.groups.ToList();
    return View(CM);
}

[HttpPost]
public ActionResult SelectClient(int? SelectedGroupId)
{
    ContractManager CM = new ContractManager();
    CM.IClients = new List();
    if (SelectedGroupId.HasValue)
    {
        CM.IClients = db.client.ToList();
    }
    return PartialView("SelectClient", CM);
}

Now The problem: 现在的问题:

In Debugging Mode when I'm selecting the first DDL. 当我选择第一个DDL时,在“调试模式”下。 Control is coming to second DDL with the values but the Second DDL is not appearing on the View. 控件将使用值进入第二个DDL,但第二个DDL未出现在视图上。 ie We can't see the Second DDL on the UI. 即,我们在用户界面上看不到第二个DDL。

I have already added ajax script in my Layout and also enabled in web.config . 我已经在布局中添加了ajax脚本,并在web.config启用了该脚本。 I'll filter the Second DDL the once if I can overcome this. 如果可以克服,我将过滤第二个DDL。

Besides of perfection the example, you can do something like this in _GroupDDL partial view with jQuery.post : 除了完善示例之外,您还可以使用jQuery.post在_GroupDDL部分视图中执行以下操作:

<script type="text/javascript">
    $('#SelectedGroupId').change(function () {
        $.post('@Url.Action("SelectClient", "Contracts")', {SelectedGroupId: $(this).val()}, function(data) {
           $('#SelectedClientId').html(data);
        });
    });
</script> 

It will call the SelectClient action of Controller each time you change the SelectedGroupId ddl, it will return the html of SelectedClientId with all options based on parent/master ddl selection. 每次更改SelectedGroupId ddl时,它将调用Controller的SelectClient操作,它将返回SelectedClientId的html和所有基于父/主ddl选择的选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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