MVC 3 Ajax.beginform提交-引起完整的回发

问题描述

|| 嗨,我正在尝试使用MVC 3处理ajax.beginform。 我有2个表单,一个工作正常的HTML.BeginForm和一个在局部视图中的Ajax.BeginForm。 Ajax表单的发布是为模型添加注释。 问题是ajax提交是在主视图中提交包括HTML.BeginForm在内的整个页面,并且不执行带有注释的异步发布。 谁能看到我做错了什么? 香港专业教育学院看看其他问题,并禁用了web.config等不干扰人的javascript。 视图:
<div id=\"maincontent\">
@using (Html.BeginForm())
{
  @Html.ValidationSummary(true)
  <fieldset>
    <legend>Edit Franchise</legend>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.Name)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.Name)
      @Html.ValidationMessageFor(model => model.Name)
    </div>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.FirstLine)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.FirstLine)
      @Html.ValidationMessageFor(model => model.FirstLine)
    </div>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.SecondLine)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.SecondLine)
      @Html.ValidationMessageFor(model => model.SecondLine)
    </div>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.City)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.City)
      @Html.ValidationMessageFor(model => model.City)
    </div>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.Postcode)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.Postcode)
      @Html.ValidationMessageFor(model => model.Postcode)
    </div>
    <div class=\"editor-label\">
      @Html.LabelFor(model => model.Telephone)
    </div>
    <div class=\"editor-field\">
      @Html.EditorFor(model => model.Telephone)
      @Html.ValidationMessageFor(model => model.Telephone)
    </div>
    <p class=\"clear\">
      <input type=\"submit\" value=\"Save\" />
    </p>
  </fieldset>

  @Html.Partial(\"Partial/_AddNote\",new Cash4Schools.Domain.Model.Note())

}
部分视图:
@model Cash4Schools.Domain.Model.Note
<script src=\"@Url.Content(\"~/Scripts/MicrosoftAjax.js\")\" type=\"text/javascript\"></script>
<script src=\"@Url.Content(\"~/Scripts/MicrosoftMvcAjax.js\")\" type=\"text/javascript\"></script>  

@using (Ajax.BeginForm(\"AddNote\",\"Franchises\",new AjaxOptions {
     HttpMethod = \"POST\",UpdateTargetId = \"note\" },new { id = \"ajaxForm\" })
     )
{
  @Html.ValidationSummary(true)
   <fieldset>
       <legend>Add a Note</legend>

      <div class=\"editor-label\">
        @Html.LabelFor(model => model.Content)
      </div>
      <div class=\"editor-field\">
        @Html.EditorFor(model => model.Content)
        @Html.ValidationMessageFor(model => model.Content)
    </div>

    <div class=\"editor-label\">
        @Html.LabelFor(model => model.CreationDate)
    </div>
    <div class=\"editor-field\">
        @Html.EditorFor(model => model.CreationDate)
        @Html.ValidationMessageFor(model => model.CreationDate)
    </div>

    <div class=\"editor-label\">
        @Html.LabelFor(model => model.CreatedBy)
    </div>
    <div class=\"editor-field\">
        @Html.EditorFor(model => model.CreatedBy)
        @Html.ValidationMessageFor(model => model.CreatedBy)
    </div>

    <p class=\"clear\">
        <input type=\"submit\" value=\"Add\" />
    </p>
</fieldset>
} 控制器:
[HttpPost]
public ActionResult AddNote(Note model)
{
  var franchise = _franchiseRepository.FindById(model.Id);

  franchise.Notes.Add(
    new Note {
      Content = model.Content,CreationDate = DateTime.Now,CreatedBy = model.CreatedBy,Type = NoteType.Franchise
    }
  );

  _franchiseRepository.Save(franchise);

  return PartialView(franchise.Notes);
}
HTML输出:
<form action=\"/Franchises/AddNote?Length=10\" data-ajax=\"true\" data-ajax-method=\"POST\" data-ajax-mode=\"replace\" data-ajax-update=\"#note\" id=\"ajaxForm\" method=\"post\">    <fieldset>
    <legend>Add a Note</legend>
    <div class=\"editor-label\">
        <label for=\"Content\">Content</label>
    </div>
    <div class=\"editor-field\">
        <input class=\"text-box single-line\" id=\"Content\" name=\"Content\" type=\"text\" value=\"\" />
        <span class=\"field-validation-valid\" data-valmsg-for=\"Content\" data-valmsg-replace=\"true\"></span>
    </div>
    <div class=\"editor-label\">
        <label for=\"CreationDate\">CreationDate</label>
    </div>
    <div class=\"editor-field\">
        <input class=\"text-box single-line\" data-val=\"true\" data-val-required=\"The CreationDate field is required.\" id=\"CreationDate\" name=\"CreationDate\" type=\"text\" value=\"01/01/0001 00:00:00\" />
        <span class=\"field-validation-valid\" data-valmsg-for=\"CreationDate\" data-valmsg-replace=\"true\"></span>
    </div>
    <div class=\"editor-label\">
        <label for=\"CreatedBy\">CreatedBy</label>
    </div>
    <div class=\"editor-field\">
        <input class=\"text-box single-line\" id=\"CreatedBy\" name=\"CreatedBy\" type=\"text\" value=\"\" />
        <span class=\"field-validation-valid\" data-valmsg-for=\"CreatedBy\" data-valmsg-replace=\"true\"></span>
    </div>

    <p class=\"clear\">
        <input type=\"submit\" value=\"Add\" />
    </p>

</fieldset>
</form><div id=\"note\"></div>
    

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)