MVC Ajax - 部分视图获取值但不刷新主视图 [英] MVC Ajax - Partial view is getting values but not refreshing the Main view

查看:92
本文介绍了MVC Ajax - 部分视图获取值但不刷新主视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手。

当我们选择第一个我们需要更新第二个的时候,我有两个下拉菜单。

以下是我的观点

@model HostingManager.Models.ContractManager

@ {

ViewBag.Title =CustomCreate;

}



@ Html.Partial(_ GroupDDL,型号)





@ Html.Partial(SelectClient,型号)





@ Html.Partial( SelectContract,型号)



以下是第一次下拉(部分查看:_GroupDDL)

@ model HostingManager.Models.ContractManager



@using(Ajax.BeginForm(SelectClient,Contracts,new AjaxOptions {UpdateTargetId =IClients})) />
{

@ Html.DropDownListFor(

m => m.SelectedGroupId,

新的SelectList(Model.IGroups, id,name),

string.Empty



}

<脚本类型=text / javascript>

$(''#SelectedGroupId'')。change(function(){

$(this).parents(''form '')。submit();

});

< / script>



以下是第二次下拉(部分查看:SelectClient)

@model HostingManager.Models.ContractManager



@if(Model.IClients!= null&& Model.IClients.Count()> 0)

{

使用(Ajax.BeginForm(SelectContracts,Contracts,新的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>



以下是我的控制器动作

public ActionResult CustomCreate()

{

ContractManager CM = new ContractManager();

CM.IGroups = db.groups.ToList();

返回视图(CM);

}



[HttpPost]

公共ActionResult SelectClient(int? SelectedGroupId)

{

ContractManager CM = new ContractManager();

CM.IClients = new List();

if(SelectedGroupId.HasValue)

{

CM.IClients = db.client.ToList();

}

返回PartialView(SelectClient,CM);

}

现在问题:

在调试模式中,当我选择第一个DDL ..控件来到第二个DDL的值,但第二个DDL没有出现在View.I意味着在UI上我们看不到第二个DDL。 />


按照我的代码调用Controller的SelectClient,但我的主视图没有使用更新的部分视图进行更新。



任何人都可以告诉问题在哪里..

我已经在我的布局中添加了ajax脚本并在web.config中启用了

我会过滤如果我能克服这个问题,那么第二个DDL就是一次。



先谢谢

I''m new to MVC.
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)
@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)
@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..Control is coming to second DDL with the values but the Second DDL is not appearing on the View.I mean on UI we can''t see the Second DDL.

As per My Code It is calling the SelectClient of Controller but my Main View is not updating with the updated partial view.

Can anybody plz tell where the problem is..
I already added ajax script in my Layout and also enabled in web.config
I''ll filter the Second DDL the Once if I can overcome this.

Thanks in Advance

推荐答案

(''#SelectedGroupId'')。change(function(){
(''#SelectedGroupId'').change(function () {


(this).parents(''form'')。submit() ;

});

< / script>



以下是第二次下拉(部分查看:SelectClient)

@model HostingManager.Models.ContractManager



@if(Model.IClients!= null&& Model.IClients.Count()> 0)

{

使用(Ajax.BeginForm(SelectContracts,Contracts,新的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>
(this).parents(''form'').submit();
});
</script>

Below is Second Drop-down(Partial View : 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(){
(''#SelectedClientId'').change(function () {


这篇关于MVC Ajax - 部分视图获取值但不刷新主视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆