是否可以通过Ajax在Mvc 4中检测表单重新加载?对不起,但我对此一无所知。 [英] Is It Possible To Detect Form Reload By Ajax In Mvc 4? Sorry But I Have No Idea About It.
问题描述
我想通过ajax检测页面重新加载,如果页面已经重新加载,那么我想填充我的DistrictId变量。当用户从下拉列表中选择值时,会发生此重新加载。
请注意我在View中编写此代码,这是一个名为 Districtddl的下拉列表
另外
@ if(Request.HttpMethod == POST){}
无法使用
< select class = ddl id = Districtddl name = Districtddl >
< option> ----选择区---- < / 选项 >
@foreach(System.Data .DataRow dr in FarmerRegisterFormAndPrint.Models.StoredP rocedures.GetAllDistrict()。行)
{
< option id = @ dr [ DistrictId ]。ToString()> @dr [ DistrictName]。ToString()< / < span class =code-leadattribute>选项 >
}
< ; / 选择 >
Quote:当用户从下拉列表中选择值时,会发生此重新加载。
@ { var DistrictId = ;}
@if(Request.HttpMethod == POST){
var DistrictId = Request.Form [ Districtddl]。ToString();
}
Quote:然后,此变量将用作参数传递到另一个下拉列表。
如何执行此操作。
< 选择 class = ddl id = Tehsilddl >
< 选项 id = 0 > ----选择区---- < / option >
@if(!string.IsNullOrEmpty(DistrictId))
{
foreach(FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId)中的System.Data.DataRow博士。行)
{
< 选项 id = @ dr [ TehsilId]。ToString() > @dr [TehsilName]。ToString()< / option >
}
}
< / select >
$( #Districtddl)。on(' 更改',功能(){
var valueDistrictDDl = $( this )。find(' option:selected')。attr(' id');
alert(valueDistrictDDl);
var urlInUse = window 。 location 跨度> .href;
$ .ajax({
url:urlInUse,
dataType: html ,
成功:功能(数据){
alert( 重新加载)
}
});
如果这对你没有任何意义,那么请告诉我如何将jquery变量设置到这里< pre lang =c#>(System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict( DistrictId )。行)
在jquery中获取所选id很容易,但不知道如何将其传递给 DistrictId
< blockquote>( #Districtddl)。on(' 更改',功能(){
var valueDistrictDDl =
( this )。find(' 选项:选择')。attr(' id');
警报(valueDistrictDDl);
var urlInUse = window 。 location .href;
.ajax({
url:urlInUse,
dataType: < span class =code-string> html,
成功:功能(数据){
alert( 重新加载)
}
});
如果这对你没有任何意义,那么请告诉我如何将jquery变量设置到这里< pre lang =c#>(System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict( DistrictId )。行)在jquery中获取所选id很容易,但不知道如何将其传递给 DistrictId
I want to detect the page reload by ajax and if the page have been reloaded only then i want to fill my DistrictId variabl. This reload is happening when a user select value from dropdownlist.
Please note that i am writing this code in View and this is for a dropdownlist named Districtddl
Also
@if (Request.HttpMethod=="POST") {}
is not working
<select class="ddl" id="Districtddl" name="Districtddl">
<option>----Select District----</option>
@foreach (System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GetAllDistrict().Rows)
{
<option id="@dr["DistrictId"].ToString()">@dr["DistrictName"].ToString()</option>
}
</select>
Quote:This reload is happening when a user select value from dropdownlist.
@{var DistrictId = "";}
@if (Request.HttpMethod=="POST") {
var DistrictId = Request.Form["Districtddl"].ToString();
}
Quote:This variable then will be used to pass as parameter to another dropdownlist.
How i can do this.
<select class="ddl" id="Tehsilddl">
<option id="0">----Select District----</option>
@if (!string.IsNullOrEmpty(DistrictId))
{
foreach (System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId).Rows)
{
<option id="@dr["TehsilId"].ToString()">@dr["TehsilName"].ToString()</option>
}
}
</select>
$("#Districtddl").on('change', function () {
var valueDistrictDDl = $(this).find('option:selected').attr('id');
alert(valueDistrictDDl);
var urlInUse = window.location.href;
$.ajax({
url: urlInUse,
dataType: "html",
success: function (data) {
alert("reloaded")
}
});
And if any of this doesnot make any sense to you then please tell me how to set jquery variable to here
(System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId).Rows)
it is a lot easy to get the selected id in jquery but do not know how to pass that to DistrictId
("#Districtddl").on('change', function () { var valueDistrictDDl =
(this).find('option:selected').attr('id'); alert(valueDistrictDDl); var urlInUse = window.location.href;
.ajax({ url: urlInUse, dataType: "html", success: function (data) { alert("reloaded") } });
And if any of this doesnot make any sense to you then please tell me how to set jquery variable to here(System.Data.DataRow dr in FarmerRegisterFormAndPrint.Models.StoredProcedures.GettehsilAccToDistrict(DistrictId).Rows)
it is a lot easy to get the selected id in jquery but do not know how to pass that to DistrictId
这篇关于是否可以通过Ajax在Mvc 4中检测表单重新加载?对不起,但我对此一无所知。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!