[英]How to call partial view based on option change in dropdown using AJAX?
我創建了父視圖和部分視圖。 在父視圖中,我有國家/地區下拉列表。 它也顯示了局部視圖。 頁面加載時,它會顯示下拉列表和部分視圖列表。
我想要的是,當我更改下拉列表中的選項時,部分視圖將根據所選的選項顯示。
我試過這段代碼,但到目前為止沒有運氣。 我已經粘貼了我嘗試過的所有代碼。 我收到的數據是return clsStakes;
如果我改變了下拉但是部分沒有反映新記錄。
請指導我。
我的模態
public class clsStakes
{
public string Country { get; set; }
}
public class ClsPartialStackes
{
public string Date { get; set; }
public string Race { get; set; }
}
調節器
public class HomeController : Controller
{
[HttpGet]
public ActionResult Home()
{
return View();
}
// Display Partial View
public ActionResult PartialView(string countrylist, clsStakes clsStakes)
{
if(countrylist==null)
{
clsStakes.Country = "IRE";
}
else
{
clsStakes.Country = countrylist;
}
StakesDetails stakesDetails = new StakesDetails();
return PartialView("~/Views/Stakes/_PartialStakes.cshtml", stakesDetails.StacksList(clsStakes.Country));
}
}
視圖
@model AAA.Website.Models.Stakes.clsStakes
@Html.DropDownListFor(m => m.Country, new List<SelectListItem>
{
new SelectListItem {Value = "IRE", Text="Ireland" },
new SelectListItem {Value = "ITY", Text = "Italy"},
new SelectListItem {Value = "JPN", Text = "Japan" },
new SelectListItem {Value = "NZ", Text = "New Zealand" },
},
new {@id="CountryList", @class = "form-control" })
<div id="mypartial"> </div>
調用過程以加載部分項目列表的方法
public class StakesDetails
{
clsUtilities clsUtilities = new clsUtilities();
DataSet ds;
public List<ClsPartialStackes> StacksList(string Country)
{
List<ClsPartialStackes> clsStakes = new List<ClsPartialStackes>();
SqlParameter[] prms = new SqlParameter[1];
string sSQL;
sSQL = "exec GetStakes @Country";
prms[0] = new SqlParameter("@Country", SqlDbType.VarChar);
prms[0].Value = Country;
ds = clsUtilities.CreateCommandwithParams(sSQL, prms);
DataTable dataTable = ds.Tables[0];
foreach (DataRow dr in dataTable.Rows)
{
clsStakes.Add(
new ClsPartialStackes
{
Date = Convert.ToString(dr["mdate"]),
Race = Convert.ToString(dr["racename"]),
});
}
return clsStakes;
}
}
腳本加載部分
$(document).ready(function () {
var route = '@Url.Action("PartialView", "Home")';
route = encodeURI(route);
$('#mypartial').load(route);
});
PartialView
@model IEnumerable<AAA.Website.Models.Stakes.ClsPartialStackes>
<table>
<tr>
<th>@Html.DisplayNameFor(m => m.Date)</th>
<th>@Html.DisplayNameFor(m=>m.Race)</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Race)
</td>
</tr>
}
</table>
根據下拉列表中的更改調用partial的腳本
$('#CountryList').change(function () {
var countrylist = $(this).val();
$.ajax({
url: '@Url.Action("PartialView", "Home")',
type: 'POST',
data: JSON.stringify({ countrylist: countrylist }),
dataType: 'json',
contentType: 'application/json',
success: function (data) {
$("#mypartial").html(data);
}
});
});
從MVC請求部分視圖時,您將返回在服務器上呈現的HTML,因此您的ajax請求不得請求json:
$.ajax({
url: '@Url.Action("PartialView", "Home")',
type: 'POST',
data: JSON.stringify({ countrylist: countrylist }),
dataType: 'json',
contentType: 'application/json',
success: function (data) {
$("#mypartial").html(data);
}
});
刪除行:
dataType: 'json',
或將其更改為'html'
贈送:
$.ajax({
url: '@Url.Action("PartialView", "Home")',
type: 'POST',
data: JSON.stringify({ countrylist: countrylist }),
contentType: 'application/json',
success: function (data) {
$("#mypartial").html(data);
}
});
您的代碼中有一個拼寫錯誤:
$("#mypartial".html(data);
並將數據類型和內容類型更改為
$('#CountryList').change(function () {
var countrylist = $(this).val();
$.ajax({
url: '@Url.Action("PartialView", "Home")',
type: 'POST',
data: JSON.stringify({ countrylist: countrylist }),
contentType: 'application/json; charset=utf-8',
success: function (data) {
$("#mypartial").html(data);
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.