0
在我的觀點之一 - 評分控制器的索引視圖中,我顯示了一些圖像並使它們可以使用javascript進行點擊。當圖像被點擊時,我填充一個數組。一旦用戶點擊提交按鈕,該數組將作爲字符串傳遞給評分控制器中的Score方法。在這種方法中,我可以設置所有數據,並且它看起來像使用Chrome調試器查看「網絡」選項卡時顯示新的「分數」視圖......但是當我查看實際的瀏覽器沒有新的顯示。我仍然可以看到和以前一樣的圖像列表。爲什麼我的視圖不能正確渲染?MVC4中的HttpPost方法沒有返回正確的視圖
得分/ Index.cshtml JS
$(document).ready(function() {
$(function() {
$("img").click(function() {
// Add and remove border for image
if ($(this).hasClass('selectedCard')) {
$(this).removeClass('selectedCard');
for (var i = 0, len = cardIdList.length; i < len; i++) {
if (cardIdList[i] === $(this).attr('id')) {
cardIdList.splice(i, 1);
alert(cardIdList.toString());
break;
}
}
}
else
{
$(this).addClass('selectedCard');
var id = $(this).attr('id');
cardIdList = (typeof cardIdList != 'undefined' && cardIdList instanceof Array) ? cardIdList : [];
cardIdList.push(id);
alert(cardIdList.toString());
}
});
$(".button").on('click', function() {
$.post("@Url.Action("Score", "Scoring")", { CardIds: cardIdList.toString()});
});
});
});
ScoringController
public ActionResult Index()
{
var cards = cardDB.Cards.ToList();
return View(cards);
}
[HttpPost]
public ActionResult Score(string CardIds)
{
// splits up CardIds string into List
List<string> cardIdList = new List<string>();
cardIdList.AddRange(CardIds.Split(new char[] { ',' }));
CardDeck deckToScore = new CardDeck();
List<Card> cardsToAdd = new List<Card>();
for (int i = 0, len = cardIdList.Count(); i < len; i++)
{
var card = cardDB.Cards.Find(Convert.ToInt32(cardIdList[i]));
cardsToAdd.Add(card);
}
deckToScore.Deck = cardsToAdd;
return View(deckToScore);
}
}
Score.cshtml
@model GoStopPrimer.Models.CardDeck
@ { ViewBag.Title = 「分數」; }
@for (int i = 0, len = @Model.Deck.Count; i < len; i++)
{
<img alt= @Model.Deck[i].Name, src= @Model.Deck[i].CardArtUrl id= @Model.Deck[i].CardId />
}
原來我回來了「的意見」 POST方法......你說得對,我沒有做任何事的。我只是希望它能呈現視圖......但我最終將它顯示在前一視圖的div上。謝謝!! –