【问题标题】:MVC - Redirect to view after "$.post" to controllerMVC - 在“$.post”之后重定向到控制器查看
【发布时间】:2017-05-20 14:58:09
【问题描述】:

点击按钮:

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

我正在调用函数:

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);
}

从那里将数据传递到控制器操作中:

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);
}

为了从以下位置生成模型(用于我试图显示的视图):

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;
}

结束于:

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

问题是我没有重定向到视图Duplicates。我只是停留在同一页面上。问题可能很明显,但我似乎无法找到它。我在单击按钮后调用AddNewAccount 后尝试重定向,但当时没有生成我的Duplicates 模型。

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    问题是您将RedirectToActionAJAX 调用一起使用,这将永远无法工作,因为RedirectToAction 将向您的AJAX 调用和您的AJAX 调用返回重定向响应HTTP 302不会像您预期的那样使用此响应,也不会发生重定向。

    要解决此问题,您需要将您的 $.post 更改为完整的回帖,或发送 url 以响应重定向并在客户端手动执行重定向,然后例如使用:window.location.href = 'new-url-to-go-to';

    【讨论】:

    • 你知道如何做回发的好文章吗?我对他们不是很熟悉。
    • 我没有看到您的HTML,但我可以猜测这些是您填写数据的一组字段,如果是这种情况,那么只需将它们放入一个表单并使其成为可发布的表单使用@Html.BeginForm 就像提到的here
    猜你喜欢
    • 2016-10-08
    • 1970-01-01
    • 2017-04-26
    • 2019-08-31
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多