【问题标题】:MVC Action returning either Actionresult leaving the view unmodified or a View actionresultMVC Action 返回 Actionresult 保持视图未修改或 View actionresult
【发布时间】:2017-05-09 05:20:00
【问题描述】:

我在 MVC Razor 应用程序中的问题: 我使用基于 javascript 计时器的轮询控制器操作调用来请求是否已经有答案(顺便说一句,通过Controller 调用中的 WCF 发生)。

如果有答案,视图会通过正常返回 View(...) 操作结果更新,这会正确更新视图。 但如果答案仍然不可用,我想要一个不会更改或更新View 的 ActionResult。我试图返回EmptyResult,但后来我的网页变白了。如果可能,我不想修改我的控制器操作调用方法:

 @using (Html.BeginForm("actionTryGetNewSystemSentence", "Conduct", FormMethod.Post, 
        new { onsubmit = "OnTryGetNewSystemSentence(event)", id = "idConductForm" })) 
        { ...}

我还尝试了其他ActionResult 类型,但它们要么修改视图,要么返回空白或无效View。轮询每 500 毫秒 完成一次,所以我不能让视图每 500 毫秒 更新一次。

【问题讨论】:

  • 您是尝试基于计时器发出 ajax 请求还是只是重新加载页面?

标签: c# asp.net-mvc razor actionresult


【解决方案1】:

您在这里使用的是 Html.BeginForm,这意味着它不是 ajax 调用。您必须指定 Ajax.BeginForm 并在 jquery 中定义一个函数,当结果为成功或失败时您将调用该函数并相应地更新 html。

@using (Ajax.BeginForm("actionTryGetNewSystemSentence", "Conduct", new AjaxOptions  { HttpMethod = "POST", onsubmit = "OnTryGetNewSystemSentence(event)", OnSuccess = "Successmethod(event)", OnFailure = "Failuremethod(event)", id = "idConductForm" })) { ...})

查看下面的链接

http://www.c-sharpcorner.com/UploadFile/0c1bb2/ajax-beginform-in-Asp-Net-mvc-5/

【讨论】:

  • 非常感谢!看起来很有希望。我会在这周晚些时候试试。
  • 我还有很多其他问题,但你的回答让我朝着正确的方向前进。现在我有一个烦人的问题,我无法从 js 刷新/重新加载我的剃须刀页面。
猜你喜欢
  • 2023-03-24
  • 1970-01-01
  • 2013-08-06
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
相关资源
最近更新 更多