簡體   English   中英

Json Url.Action在新的MVC 5應用中失敗

[英]Json Url.Action failing in new MVC 5 app

我在應用程序中廣泛使用return Json(new { url = Url.Action("MyAction", new { param = 1 } 。最近我將MVC 4應用程序遷移到MVC 5,但仍然可以使用,但是如果我構建一個帶有Visual Studio 2013 Update 2的新MVC應用程序(該模板以MVC 5,EF 6.1和Identity 2應用程序為模板),現在無法重定向,並且json發送到瀏覽器:

Do you want to open or save 23xdf543v.json (28 bytes) from localhost

(要獲得Identity 2設置,我創建了一個新的MVC 5應用程序,並將所有MVC 4應用程序文件復制到其中,並進行了重構以使其正常運行)。

如果我打開JSON,則該網址似乎格式正確。 (在此示例中,MyAction在MyController中)

{"url":"/MyController/MyAction/1"}.

奇怪的是,它可以與從MVC 4遷移到MVC 5的應用程序一起使用,但不能與新模板化的MVC 5的應用程序一起使用。

行動:

public ActionResult Phn2Add(PhoneVm vM)
{ 
  if (ModelState.IsValid)
  { 
    _db.Phones.Add(vM.Phone); _db.SaveChanges();
    return Json(new { url = Url.Action("Details", new { id = vM.DetailsId }) });
  }
  ModelState.AddModelError(string.Empty, ErMsInvldEdt);
  PhTyDrpDwn();
  return PartialView("Add_Del/_PhnAdd", vM);
}

視圖:

@model MyApplicaiton.ViewModels.PhoneVm
@{ ViewBag.Title = "Add"; } 
<script type="text/javascript">
  var onSuccess = function (result) { if (result.url) { window.location.href = result.url; } }
</script>
<div id="PhnAdd">
  <div style="padding-left:17px">
    <h3>Add a @Model.Msg? @Html.ActionLink("CANCEL", null, new { id = Model.DetailsId }) </h3>
  </div>
  @using (Ajax.BeginForm(@Model.ActnNm, new AjaxOptions
    { 
      OnSuccess = "onSuccess", UpdateTargetId = "PhnAdd", HttpMethod = "Post"
    }))
    {  @Html.AntiForgeryToken() @Html.ValidationSummary(true) 
      <fieldset>      <legend></legend><br />
        @Html.HiddenFor(m => m.Phone.PersonId) @Html.HiddenFor(m => m.DetailsId)
        @Html.HiddenFor(m => m.ActnNm) @Html.HiddenFor(m => m.Msg)
        <table>
          <tr>
            <th><span class="editor-label">Number: (7573334444)</span></th>
            <td>
              <span class="editor-field">@Html.EditorFor(m => m.Phone.Number) @Html.ValidationMessageFor(m => m.Phone.Number) </span>
            </td>
          </tr>
          <tr>
            <th><span class="editor-label">Type: Land or Mobile</span></th>
             <td><span class="editor-field">@Html.DropDownList("Phone.Type", (IEnumerable<SelectListItem>)ViewBag._PhTyp)
                                            @Html.ValidationMessageFor(m => m.Phone.Type) </span>
             </td>
          </tr>
        </table>
        <input type="submit" value="Add" />
      </fieldset>
    }
  </div>

MVC 5處理Url.Action是否Url.Action不同? javascript對OnSuccess的處理是否有變化? 是否有需要更改的設置?

Visual Studio 2013更新2,MVC模板未安裝jquery.unobstrusive-ajax.js腳本。 我在BundleConfig中添加了它,但未將其安裝在Script文件夾中。 安裝后,AjaxOnSuccess開始工作。

這兩個腳本已從VS2013的模板中刪除。 我已經與編輯/模板團隊進行了交談,當我獲得有關該計划的更多信息時,我們將在此處進行更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM