如何在mvc4中处理下拉列表的更改事件?

时间:2018-05-10 11:19:22

标签: jquery asp.net-mvc

我在父视图中有下拉列表,并且还有多个部分视图。当用户更改下拉列表的值时,所有部分值都应根据下拉列表中的新值进行更改。请看一下我的剧本。我觉得有些东西不见了。

当页面首次加载时,我正在使用ActionResult,它在下拉列表更改事件执行时是相同的。所以同样的ActionResult调用了两次。

错误

  

无法加载资源:服务器响应状态为404   (未找到)

模型

public class ClsA
{  public string Country{get;set;}      
    public List<first> firsts{ get; set; }    
    public List<second> seconds{ get; set; }
    public List<third> thirds{ get; set; }
}

public class first
{     
    public string NameId { get; set; }
    public string Address { get; set; }

}

public class second
{
    public string Details{ get; set; }
    public string Age{ get; set; }             
}

public class third
{
    public string occupation{ get; set; }
    public string company{ get; set; }             
}

控制器

[HttpGet]
public ActionResult ABC(string countrylist)
 {
   SDetails sDetails=new SDetails(); 
   string nameid = sDetails.Rst(countrylist).FirstOrDefault().NameId;       
   var model = new ClsA();
   model.firsts = sDetails.Rst(countrylist);            
   model.seconds = sDetails.Rs(nameid); 
   model.thirds = sDetails.Ss(nameid);  
   return View(model);
 }

查看

@model Aplication.Models.ABC.ClsA     
@Html.DropDownListFor(m => m.Country, new List<SelectListItem>
{                       
    new SelectListItem {Value = "NP", Text="NLand" },
    new SelectListItem {Value = "MS", Text = "MaLand"},    
},new { @id = "CountryList"})

<div id=myA>
    @{ Html.RenderPartial("_PartialA", Model.firsts); }
</div>
<div id=myB>
    @{ Html.RenderPartial("_PartialB", Model.seconds);} 
</div>
<div id=myC>
    @{ Html.RenderPartial("_PartialC", Model.thirds);} 
</div>

脚本

  $('#CountryList').change(function () {
                var countrylist = $(this).val();
                var url = '@Url.Action("ABC", "ABC")';
                $('#myA').load(url, { countrylist: countrylist });
                $('#myB').load(url, { countrylist: countrylist });
                $('#myC').load(url, { countrylist: countrylist });
            });

_PartialA

@model  IEnumerable<Aplication.Models.ABC.first>

    <table>
        <tr>
           <th>@Html.DisplayNameFor(m => m.NameId)</th>
            <th>@Html.DisplayNameFor(m => m.Address)</th>            
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.NameId)
                </td>
                <td>                   
                    @Html.DisplayFor(modelItem => item.Address)                    
                </td>                

            </tr>
        }
    </table>

_PartialB

@model  IEnumerable<Aplication.Models.ABC.second>

    <table>
        <tr>
           <th>@Html.DisplayNameFor(m => m.Details)</th>
            <th>@Html.DisplayNameFor(m => m.Age)</th>            
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Details)
                </td>
                <td>                   
                    @Html.DisplayFor(modelItem => item.Age)                    
                </td>                

            </tr>
        }
    </table>

_PartialC

@model  IEnumerable<Aplication.Models.ABC.third>

    <table>
        <tr>
           <th>@Html.DisplayNameFor(m => m.occupation)</th>
            <th>@Html.DisplayNameFor(m => m.company)</th>            
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.occupation)
                </td>
                <td>                   
                    @Html.DisplayFor(modelItem => item.company)                    
                </td>                

            </tr>
        }
    </table>

0 个答案:

没有答案