简体   繁体   English

MVC-将“ $ .post”后的视图重定向到控制器

[英]MVC - Redirect to view after “$.post” to controller

On the click of a button: 单击按钮:

$("#newSave").click(function () {
    AddNewAccount();
});

I am making a function call to: 我正在对以下对象进行函数调用:

function AddNewAccount() {
    $.post("/RxCard/AddAccount",
        {
            NewAccountName: $("#newAccountName").val(),
            NewAccountAddress: $("#newAccountAddress").val(),
            NewAccountCity: $("#newAccountCity").val(),
            NewAccountState: $("#newAccountState").val(),
            NewAccountZip: $("#newAccountZip").val(),
            NewAccountArea: $("#newArea").val(),
            NewAccountPrefix: $("#newPrefix").val(),
            NewAccountSuffix:$("#newSuffix").val()

        }, function (errorMsg) {
            if (errorMsg.length > 0) {
                $('div#divSearchList').empty();
                $('div#divSearchList').html(errorMsg);
            }
            else {
                loadAccount(accountId, false);
            }
    });
    $("body").css("cursor", "default").delay(1000);
}

from there the data is passed into the controller action: 从那里数据被传递到控制器动作:

public ActionResult AddAccount(string NewAccountName, string NewAccountAddress, string NewAccountCity, string NewAccountState, string NewAccountZip, string NewAccountArea, string NewAccountPrefix, string NewAccountSuffix)
{
     DuplicatesPage model = GetDups(NewAccountName, NewAccountAddress, NewAccountCity, NewAccountState, NewAccountZip, NewAccountArea, NewAccountPrefix, NewAccountSuffix);
     return RedirectToAction("DuplicatePage", model);
}

in order to generate the model (for the view i am attempting to display) from: 为了从以下位置生成模型(对于我尝试显示的视图):

private DuplicatesPage GetDups(string NewAccountName, string NewAccountAddress, string NewAccountCity, string NewAccountState, string NewAccountZip, string NewAccountArea, string NewAccountPrefix, string NewAccountSuffix)
{

    DuplicatesPage model = new DuplicatesPage();
    duplicates results = new duplicates();
    var phone = NewAccountPrefix.ToString() + NewAccountSuffix.ToString();
    var datenow = DateTime.Now;
    var firstofmonth = new DateTime(datenow.Year, datenow.Month, 1);

    OdbcCommand cmd1 = new OdbcCommand();

    cmd1.CommandText = "{call dbo.Maint_AddClinic(?,?,?,?,?,?,?,?,?)}";
    cmd1.Parameters.AddWithValue("@Name", NewAccountName);
    cmd1.Parameters.AddWithValue("@Address", NewAccountAddress);
    cmd1.Parameters.AddWithValue("@City", NewAccountCity);
    cmd1.Parameters.AddWithValue("@State", NewAccountState);
    cmd1.Parameters.AddWithValue("@Zip", NewAccountZip);
    cmd1.Parameters.AddWithValue("@AreaCode", NewAccountArea);
    cmd1.Parameters.AddWithValue("@PhoneNumber", phone);
    cmd1.Parameters.AddWithValue("@StartDate", firstofmonth);
    cmd1.Parameters.AddWithValue("@AccountTypeID", 2);
    cmd1.Parameters.AddWithValue("@IgnoreDupAddress", 1);
    DataTable dt = GetData(cmd1);
    foreach (DataRow row in dt.Rows)
    {
        Pharmacy pharmacy = new Pharmacy();
        pharmacy.AccountName = row["AccountName"].ToString();
        pharmacy.Address = row["Address"].ToString();
        pharmacy.City = row["City"].ToString();
        pharmacy.ZipCode = row["ZipCode"].ToString();
        results.Pharmacies.Add(pharmacy);
    }
    results.Count = dt.Rows.Count;
    model.DupResults = results;
    return model;
}

and end at: 并结束于:

public ActionResult DuplicatePage()
{
     return View("Duplicates");
}

The problem is that I am not redirected to the view Duplicates . 问题是我没有重定向到视图Duplicates I just stay on the same page. 我只是停留在同一页面上。 The problem is probably obvious but I cant seem to locate it. 这个问题可能很明显,但是我似乎找不到它。 I tried redirecting after making the call to AddNewAccount on the button click but my model for Duplicates is not generated at that time. 在单击按钮对AddNewAccount进行调用之后,我尝试了重定向,但是当时没有生成我的Duplicates模型。

The problem is that you are using RedirectToAction with an AJAX call and that will never work because the RedirectToAction will return a redirect response HTTP 302 to your AJAX call, and your AJAX call will not consume this response as you expect, and no redirects will ever happen. 问题是您正在将RedirectToActionAJAX调用一起使用,将永远无法工作,因为RedirectToAction将向AJAX调用返回重定向响应HTTP 302 ,并且您的AJAX调用不会像您期望的那样使用此响应,并且永远不会进行重定向发生。

To fix this you need to change your $.post to a full post back, or send the url to redirect to with a response and do the redirect manually on the client side and then for example use: window.location.href = 'new-url-to-go-to'; 要解决此问题,您需要将$.post更改为完整的回发,或发送url以响应将其重定向到并在客户端手动进行重定向,然后例如使用: window.location.href = 'new-url-to-go-to';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM