客户端数据注释不适用于包含我的asp.net mvc中的ajax.beginform的部分视图

时间:2012-03-04 21:49:34

标签: ajax asp.net-mvc partial-views

我有以下部分视图,点击ajax.actionlink后会返回: -

@model System.Models.Answer 
@{ 
    ViewBag.Title = "Create"; 
} 
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
 <div id = "partialWrapper">
 @using (Ajax.BeginForm("Create", "Answer", new AjaxOptions
{
    HttpMethod = "Post",
    InsertionMode = InsertionMode.InsertAfter,
    UpdateTargetId = "incrementanswer",
    OnSuccess = "removePartial"
}))

 {
     <div id = "returnedquestion">
     @Html.ValidationSummary(true)

    <fieldset> 
        <legend>Answer here</legend> 
        <div class="editor-label"> 
            @Html.LabelFor(model => model.Description) 
        </div> 
        <div class="editor-field"> 
            @Html.EditorFor(model => model.Description) 
            @Html.ValidationMessageFor(model => model.Description) 
        </div> 
        <div class="editor-label"> 
            @Html.LabelFor(model => model.IsRight) 
        </div> 
        <div class="editor-field"> 
             @Html.DropDownList("IsRight", String.Empty) 
            @Html.ValidationMessageFor(model => model.IsRight) 
        </div>  
    </fieldset> 
     <input type= "hidden" name = "questionid" value = @ViewBag.questionid>
     <input type= "hidden" name = "assessmentid" value = @ViewBag.assessmentid>
     <input type="submit" value="Add answer" />
 </div>
 }
 </div>

问题是,如果我在将所需文件留空的情况下单击提交按钮,则不会显示客户端验证,即需要IsRight和描述字段,而是整个视图(包含布局)将被退回,那么可能是什么问题?

这是从partila视图调用的action方法: -

[HttpPost] 
        public ActionResult Create(int questionid, Answer a) 
        { 
            if (ModelState.IsValid) 
            { 
        repository.AddAnswer(a); 
             repository.Save(); 
             return PartialView("_details",a); 
            } 
            return View(a); 

和将返回局部视图的ajax.actionlink: -

<div id ="link">
@Ajax.ActionLink("Add New Answer", "Create", "Answer",
        new { questionid = Model.QuestionID },
          new AjaxOptions
          {

              HttpMethod = "Get",
              UpdateTargetId = "link",
              InsertionMode = InsertionMode.InsertAfter,
              LoadingElementId = "progress"


                        })
                        </div>

3 个答案:

答案 0 :(得分:1)

您需要在ajax请求的成功部分重新绑定Unptrusive JavaScript。如下:

$.validator.unobtrusive.parse($(“div#divWizard”));

请查看以下link to my blog以获取进一步参考。

答案 1 :(得分:0)

您是否启用了不显眼的验证?

 <appSettings>
  <add key="ClientValidationEnabled" value="true"/> 
  <add key="UnobtrusiveJavaScriptEnabled" value="true"/> 
 </appSettings>

您是否正确链接了这些脚本文件?

  <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script>
  <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>
  <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"><script>

答案 2 :(得分:0)

这个答案应该是你要找的:Unobtrusive validation doesn't work with Ajax.BeginForm