我如何使用booleanwhen my dataTypeis html?當我controller檢測到 my 上沒有資料時sql,它應該像false在 mycontroller中那樣作業,我只需要boolean在我ajax的 as中拋出false。我知道使用 anhtml是自動的true,就像我使用operator. 它回傳為true. 但我怎樣才能做到這false一點呢?
這是我的控制器:
if (Reader.HasRows == true)
{
return View("TrueViewPort");
} else
{
return View("FalseViewPort");
}
阿賈克斯:
$.ajax({
url: "UrlToController",
type: "GET",
data: {
uname: uname
},
dataType: "html",
success: (function (result) {
if (result == true) { //even if its false, it always throw here
$('#wishlist-carousel').html(result); //if data detected carousel should be called
//Something elements when the data is true
} else {
//Something elements when the data is false
$(".no-page").html(result); //if no data. no carousel should detect
}
}),
error: (function (xhr, status) {
alert(status);
})
})
編輯:
這些應該出現在我的TrueViewPort:
@if (Model != null && Model.Rows.Count != 0)
{
@for (int i = 0, x = 1; i < Model.Rows.Count; i , x )
{
<div class="item">
<div class="text-center">
<div class="card">
<div class="card-img-top">
<img src="~/images/link/@Model.Rows[i][0]" />
</div>
<br />
<br />
<div class="card-body">
@Model.Rows[i][1]
<br />
@Model.Rows[i][2]
</div>
</div>
</div>
</div>
}
}
else //When the server detected that there's no data, it should throw the else statement which is working
{
<div class="container">
<div class="text-center">
<div class="not-found-404">
404
</div>
<br />
</div>
<div class="not-found">
ITEMS NOT FOUND
</div>
<br />
<div class="not-found-title">
Don't you have any want on the shop?
</div>
<br />
<div class="not-found-content">
Looks like you still haven't putting anything on your wishlist do you?
</div>
</div>
</div>
}
這是啟動它時出現在我螢屏上的內容。兩者都使用if else operators to ==

使用if as == true和else

我的.cshtml:
<div class="container">
<div class="text-center">
<div class="true">
<div class="card">
<div class="card-body">
<div class="owl-carousel owl-theme" id="wishlist-carousel"></div>
</div>
</div>
</div>
<div class="no-page"></div>
</div>
</div>
uj5u.com熱心網友回復:
你可以使用你TrueViewPort的 asif else statement你可以看到你給了你TrueViewpor2 個不同的<div>
讓我們假設你有events任何javascript
$(document).ready(function() {
$.ajax({
success: function(result) {
$(".no-page").html(result); //if no data. no carousel should detect
$('#wishlist-carousel').html(result);
//Put here your owl function
}
})
})
然后將您的更改.cshtml為:
<div class="container">
<div class="text-center">
<div class="true">
<div class="card">
<div class="card-body">
<div class="owl-carousel owl-theme" id="wishlist-carousel"></div>
</div>
</div>
</div>
</div>
</div>
<div class="no-page"></div> //separate these one, so if there's no data, the result will go here.
然后在你的TrueViewPort:
@if (Model != null && Model.Rows.Count != 0)
{
@for (int i = 0, x = 1; i < Model.Rows.Count; i , x )
{
//add a <style> here to hide the no-page.
}
}
else
{
//add a <style> here to hide the owl carousel
}
uj5u.com熱心網友回復:
您可以只回傳一個視圖并根據目標 div 中目標資料的數量進行判斷
<script src="~/lib/jquery/dist/jquery.js"></script>
<script>
$(document).ready(function () {
$.ajax({
url: "https://localhost:44332/Home/Partial",
success: function (result) {
var a = $("#test").children().length;
console.log(a);
if (a == 0) {
$(".no-page").html(result);
}
else {
$("#wishlist-carousel").html(result);
}
},
error: function (msg) {
console.log(msg);
}
})
return false;
});
</script>
<div class="container">
<div class="text-center">
<div class="true">
<div class="card">
<div class="card-body">
<div class="owl-carousel owl-theme" id="wishlist-carousel">
</div>
</div>
</div>
</div>
<div class="no-page"></div>
</div>
</div>
<div id="test">
<a>test</a>
</div>
<div id="test"></div>
結果1:洗掉
文本中有一個“測驗”文本:
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/497168.html
標籤:javascript 阿贾克斯 asp.net 核心
上一篇:無法將型別“System.Collections.Generic.IEnumerable<CommandAPI.Dtos.CommandReadDto>”隱式轉換為“Microsoft.A
