在我的一个视图中,即评分控制器的索引视图中,我显示了一些图像并使用 javascript 使它们可点击。单击图像后,我填充了一个数组。一旦用户单击提交按钮,数组就会作为字符串传递给评分控制器中的 Score 方法。在这种方法中,我可以设置所有数据,当我使用 Chrome 调试器查看“网络”选项卡时,看起来新的“分数”视图正在呈现……但是当我查看实际的浏览器时没有显示任何新内容。我仍然看到与以前相同的图像列表。为什么我的视图没有正确渲染?
评分/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()});
});
});
});
计分控制器
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);
}
}
分数.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 />
}