質問

I have the following $.ajax post call. It would go through the action being called but then it would trigger the "error" block of the function even before the actionresult finishes. Also, it seems to reload the whole page after every pass.

var pnameVal = '<%: this.ModelCodeValueHelper().ModelCode%>';
            var eidVal = '<%: ViewBag.EventId  %>';
            var dataV = $('input[ name = "__RequestVerificationToken"]').val();
            var urlVal = '<%: Url.Action("New") %>';
            alert('url > ' + urlVal);
            alert('pname - ' + pnameVal + ' eid - ' + eidVal + ' dataV = ' + dataV);

$.ajax({
                url: urlVal,
                //dataType: "JSONP",
                //contentType: "application/json; charset=utf-8",
                type: "POST",
                async: true,
                data: { __RequestVerificationToken: dataV, pname: pnameVal, eId: eidVal },
                success: function (data) {
                    alert('successssesss');
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(XMLHttpRequest);
                    alert(textStatus);
                    alert(errorThrown);
                    alert('dammit');
                }
            })
            .done(function (result) {
                if (result.Success) {
                    alert(result.Message);
                }
                else if (result.Message) {
                    alert(' alert' + result.Message);
                }
                alert('done final');
                //$('#search-btn').text('SEARCH');
                waitOff();

            });

This is the action

    [HttpPost]
    public ActionResult New(string pname, int eid)
    {
        var response = new ChangeResults { }; // this is a viewmodel class 

        Mat newMat = new Mat { "some stuff properties" };
        Event eve = context.Events.FirstOrDefault(e => e.Id == eid);
        List<Mat> mats = new List<Mat>();

        try
        {
            eve.Mats.Add(newMat);
            icdb.SaveChanges();

            mats = icdb.Mats.Where(m => m.EventId == eid).ToList();

            response.Success = true;
            response.Message = "YES! Success!";
            response.Content = mats;           // this is an object type     
        }
        catch (Exception ex)
        {
            response.Success = false;
            response.Message = ex.Message;
            response.Content = ex.Message;     // this is an object type
        }

        return Json(response);
    }

Btw, on fiddler the raw data would return the following message:

{"Success":true,"Message":"Added new Mat.","Content":[]}

And then it would reload the whole page again. I want to do an ajax call to just show added mats without having to load the whole thing. But it's not happening atm.

Thoughts?

役に立ちましたか?

解決

You probably need to add e.preventDefault() in your handler, at the beginning (I am guessing that this ajax call is made on click, which is handled somewhere, that is the handler I am talking about).

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top