2012-08-07 118 views
0

我一直在爲此奮鬥了幾天。我有一個表單,它是通過Ajax加載的部分視圖,但它不是通過Ajax提交,即使它使用Ajax.BeginForm()。Ajax.BeginForm()不通過ajax提交

控制器:

<HttpGet()> _ 
    Public Function PersonalDetailsEdit(PersonalInfo As DetailsViewModel.PersonalDetails) As PartialViewResult 
     Return PartialView(PersonalInfo) 
    End Function 

    <HttpPost()> _ 
    <AjaxOnly()> _ 
    Public Function PersonalDetailsEdit(formcollection As FormCollection, model As DetailsViewModel.PersonalDetails) As PartialViewResult 
     Return PartialView("PersonalDetails", model) 
    End Function 

查看:

@ModelType ASK.Everest.ViewModels.DetailsViewModel.PersonalDetails 

@Using Ajax.BeginForm("PersonalDetailsUpdate", "Details", New AjaxOptions() With { 
        .InsertionMode=InsertionMode.Replace, 
        .UpdateTargetId = "PersonalDetailsSection", 
        .OnFailure = "alert('fail');"}, New With {.id="PersonalDetailsForm"}) 
    @Html.ValidationSummary(True) 

@<fieldset id="PersonalDetailsUpdate"> 
    <table class="table table-striped "> 
     <tr> 
      <th class="display-label"> 
       <input type="hidden" value="@Html.NameFor(Function(model) model.Title)" /> 
       @Html.DisplayNameFor(Function(model) model.Title) 
      </th> 
      <th class="display-label"> 
       <input type="hidden" value="@Html.NameFor(Function(model) model.FirstName)" /> 
       @Html.DisplayNameFor(Function(model) model.FirstName) 
      </th> 
      <th class="display-label"> 
       <input type="hidden" value="Surname" /> 
       @Html.DisplayNameFor(Function(model) model.Surname) 
      </th> 
     </tr> 
     <tr> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.Title) 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.FirstName) 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.Surname) 
      </td> 
     </tr> 
     <tr> 
      <th> 
      </th> 
      <th class="display-label"> 
       @Html.DisplayNameFor(Function(model) model.Gender) 
      </th> 
      <th class="display-label"> 
       <input type="hidden" value="@Html.NameFor(Function(model) model.DOB)" /> 
       @Html.DisplayNameFor(Function(model) model.DOB) 
      </th> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.Gender) 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.DOB) 
      </td> 
     </tr> 
     <tr> 
      <th> 
      </th> 
      <th class="display-label" colspan="2"> 
       @Html.DisplayNameFor(Function(model) model.IDNumber) 
      </th> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td class="display-field" colspan="2"> 
       @Html.DisplayFor(Function(model) model.IDNumber) 
      </td> 
     </tr> 
     <tr> 
      <th> 
      </th> 
      <th class="display-label"> 
       @Html.DisplayNameFor(Function(model) model.MaritalStatus) 
      </th> 
      <th class="display-label"> 
       @Html.DisplayNameFor(Function(model) model.Nationality) 
      </th> 
     </tr> 
     <tr> 
      <td> 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.MaritalStatus) 
      </td> 
      <td class="display-field"> 
       @Html.DisplayFor(Function(model) model.Nationality) 
      </td> 
     </tr> 
    </table> 
    @*@Html.ActionLink("Back to List", "Index")*@ 
    <p class="pull-right"> 

     <input type="submit" value="Save" /> 
    </p> 
</fieldset> 
End Using 

我的包:

bundles.Add(New ScriptBundle("~/bundles/ask").Include(
       "~/Scripts/jquery-1.7.*", 
       "~/Scripts/jquery-ui*", 
       "~/Scripts/jquery.validate*", 
       "~/Scripts/jquery.unobtrusive*", 
       "~/Content/bootstrap/js/bootstrap.js")) 

_Layout.vbtml:

<!DOCTYPE html> 
<html lang="eng"> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width" /> 
    <title>@ViewData("Title")</title> 
    @Styles.Render("~/Content/css") 
    @RenderSection("styles", required:=False) 

    @Scripts.Render("~/bundles/modernizr") 
    @Scripts.Render("~/bundles/ask") 

    @RenderSection("scripts", required:=False) 
</head> 
<body> 
    @RenderBody() 
</body> 
</html> 

放棄了太多的無奈後,決定去JQuery的+淘汰賽路線與MVVM在客戶端這樣的:

<script type="text/javascript"> 
var data = @Html.Raw(Json.Encode(Model)) ; 
var PersonalDetailsVM = ko.mapping.fromJS(data); 
ko.applyBindings(PersonalDetailsVM, $('section#PersonalDetails')[0]); 
</script> 

現在,我只是用JSON/MVC日期轉換掙扎 - 我以爲MVC4本來是要默認使用JSON.NET?如果它很麻煩,我只會發布另一個問題。

回答