MVC4中的HttpPost方法不返回正确的视图

时间:2013-08-23 01:09:45

标签: asp.net-mvc-4 view return http-post

在我的一个视图中,评分控制器的索引视图,我显示一些图像,并使用javascript使它们可点击。单击图像后,我填充一个数组。用户单击提交按钮后,数组将作为字符串传递给Scoring控制器中的Score方法。在那种方法中,我能够设置所有数据,当我使用Chrome调试器查看“网络”选项卡时,它会像新的“得分”视图一样呈现......但是当我查看实际的浏览器时什么都没有显示出来。我仍然看到与以前相同的图像列表。为什么我的View无法正确呈现?

评分/ 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 />
}

1 个答案:

答案 0 :(得分:1)

您正在使用$.post发出AJAX请求,但没有对响应做任何事情。也许您打算为success来电提供$.post回调(或调用.done()方法传递回调)?

请参阅:http://api.jquery.com/jQuery.post/