2017-06-29 55 views
-1

这是ñasp.net mvc 5应用程序开发使用vb.net 我在视图中使用Ajax.beginform,但在控制器回调请求时,不会将其识别为ajax请求。 下面是视图在视图中使用ajax.beginform,但控制器不会识别为ajax请求

<div Class="modal-dialog"> 
    <div Class="modal-content"> 
     <div Class="modal-header"> 
      <Button type="button" Class="close" data-dismiss="modal" aria-hidden="true"></Button> 
      <h4 Class="modal-title">Edit Nome</h4> 
     </div> 
     @Using (Ajax.BeginForm("Edit", "Nomes", Nothing, 
               New AjaxOptions With {.HttpMethod = "POST", .OnSuccess = "UpdateSuccess"}, 
               New With {.Class = "form-horizontal", .role = "form"})) 

    @Html.AntiForgeryToken() 
      @<div Class="modal-body"> 
........... 
        <div class="col-md-offset-2 col-md-20"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
         <input type="submit" class="btn btn-primary" value="Save Changes" /> 
        </div> 
       </div> 
      </div> 
     End using 
    </div> 
</div> 

的代码和她是控制器

<HttpPost> 
     <ValidateAntiForgeryToken()> 
     Public Async Function Edit(modelNome As NomeVM) As Task(Of ActionResult) 

      If Not ModelState.IsValid Then 
       Response.StatusCode = CInt(HttpStatusCode.BadRequest) 
       Return View(If(Request.IsAjaxRequest(), "Edit", "Edit"), modelNome) 
      End If 

      Dim nome As Nome = MaptoModel(modelNome) 

      dbContext.Nome.Attach(nome) 
      dbContext.Entry(nome).State = EntityState.Modified 
      Dim task = dbContext.SaveChangesAsync() 
      Await task 

      If task.Exception IsNot Nothing Then 
       ModelState.AddModelError("", "Unable to update the Nome") 
       Response.StatusCode = CInt(HttpStatusCode.BadRequest) 
       Return View(If(Request.IsAjaxRequest(), "Edit", "Edit"), modelNome) 
      End If 

      If Request.IsAjaxRequest() Then 
       Return Content("success") 
      End If 

      Return RedirectToAction("Index") 

     End Function 
+0

检查浏览器的网络选项卡,看到什么类型的请求时,它是当你提交表格(xhr或不) – Shyju

回答

0

你可能缺少jquery.unobtrusive-ajax.min.js的代码。你不能使用cdn,而是去管理nuget。安装jquery.unobtrusive-ajax并引用它。

感谢Using Ajax.BeginForm with ASP.NET MVC 3 Razor

下面是一个例子

查看:

@ModelType VisualBasic.MyViewModel 
<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width" /> 
    <title>IndexValid100</title> 
    @*MAKE SURe these references are included*@ 
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script> 
    @*get nuget for jquery.unobtrusive-ajax.min, cdn is for mvc3 and won't work*@ 
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 
</head> 
<body> 
    <div id="result"></div> 
    @Using (Ajax.BeginForm("Index", New AjaxOptions With {.UpdateTargetId = "result"})) 
     @Html.EditorFor(Function(model) model.Foo) 
     @Html.ValidationMessageFor(Function(model) model.Foo) 
     @<input type="submit" value="OK" /> 
    End Using 
</body> 
</html> 

控制器/型号:

Imports System.ComponentModel.DataAnnotations 

Public Class MyViewModel 
    Public Property Foo() As String 
End Class 

Public Class HomeController 
    Inherits System.Web.Mvc.Controller 

    <HttpPost> 
    Function Index(ByVal myViewModel As MyViewModel) As ActionResult 
     Return Content("Thanks", "text/html") 
    End Function 

    Function Index() As ActionResult 
     Return View(New MyViewModel()) 
    End Function 
相关问题