简体   繁体   English

Json Url.Action在新的MVC 5应用中失败

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

I've made extensive use of return Json(new { url = Url.Action("MyAction", new { param = 1 } in my apps. I recently migrated an MVC 4 app to MVC 5 and it still works but if I build a new MVC app with Visual Studio 2013 update 2 (which templates a MVC 5, EF 6.1 and Identity 2 app) it now fails to redirect and the json is sent to the browser: 我在应用程序中广泛使用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

(To get Identity 2 setup I created a new MVC 5 app and copied all my MVC 4 app files into it and refactored to get it working). (要获得Identity 2设置,我创建了一个新的MVC 5应用程序,并将所有MVC 4应用程序文件复制到其中,并进行了重构以使其正常运行)。

If I open the JSON the url appears to be properly formatted. 如果我打开JSON,则该网址似乎格式正确。 (in this example the MyAction is in MyController) (在此示例中,MyAction在MyController中)

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

The odd thing is that it works with a migrated MVC 4 to MVC 5 app but not a newly templated MVC 5 app. 奇怪的是,它可以与从MVC 4迁移到MVC 5的应用程序一起使用,但不能与新模板化的MVC 5的应用程序一起使用。

Action: 行动:

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

View: 视图:

@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>

Does MVC 5 handle Url.Action differently? MVC 5处理Url.Action是否Url.Action不同? Is there some change in javascript's handling of OnSuccess ? javascript对OnSuccess的处理是否有变化? Is there a setting that needs to be changed? 是否有需要更改的设置?

The Visual Studio 2013, update 2, MVC template doesn't install the jquery.unobstrusive-ajax.js script. Visual Studio 2013更新2,MVC模板未安装jquery.unobstrusive-ajax.js脚本。 I Added it in my BundleConfig but it wasn't installed in the Script folder. 我在BundleConfig中添加了它,但未将其安装在Script文件夹中。 Once installed the AjaxOnSuccess started working. 安装后,AjaxOnSuccess开始工作。

These two scripts were dropped from the templates in VS2013. 这两个脚本已从VS2013的模板中删除。 I've talked to the editor/template team, and we will update here when I get more info on the plan. 我已经与编辑/模板团队进行了交谈,当我获得有关该计划的更多信息时,我们将在此处进行更新。

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

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