2012-10-15 49 views
0

我有一個DropDownList控件和一個文本框的局部視圖。在asp.net中驗證錯誤是不可見partialView mvc

當什麼也沒有選擇或輸入任何文字ModelState.IsValid是FALSE

但我可以看到任何驗證錯誤顯示的註釋上我的財產。

這是爲什麼?例如,我可以在ModelState.Value.Errors屬性中看到「缺少名稱」

「。

當我確認我的輸出窗口中的無效視圖我看到這一點:

A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll 
A first chance exception of type 'System.InvalidOperationException' occurred in System.Web.Mvc.dll 


public ActionResult Edit() 
{ 
    return LoadEditTemplates(); 
} 


[HttpPost] 
public ActionResult Update(EditTemplateListViewModel viewModel) 
{ 
    if (ModelState.IsValid && !_templateDataProvider.TemplateExists(viewModel.Name)) 
    { 
     Template template = Mapper.Map<EditTemplateListViewModel, Template>(viewModel); 
     _templateDataProvider.UpdateTemplate(template); 
     return new JsonNetResult(new { success = true }); 
    } 

    return PartialView(viewModel); 
} 

private ActionResult LoadEditTemplates() 
{ 
    var templates = _templateDataProvider.GetTemplates(); 
    EditTemplateListViewModel editTemplateViewModel = new EditTemplateListViewModel() 
    { 
     DisplayList = Mapper.Map<IEnumerable<Template>, IEnumerable<TemplateViewModel>>(templates), 
    }; 
    return PartialView(editTemplateViewModel); 
} 

public class EditTemplateListViewModel 
    { 
     [Required(ErrorMessage = "No template selected.")] 
     public int TemplateId { get; set; } 

     [Required(ErrorMessage="Name is missing")] 
     public string Name { get; set; } 

     public IEnumerable<TemplateViewModel> DisplayList { get; set; }  
    } 

@model ITMS.Web.Models.EditTemplateListViewModel 

@*Remote Validation*@ 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

@using (Html.BeginForm("Update", "Template")) 
{  
    @Html.ValidationSummary(false) 
    @Html.DropDownListFor(x => x.TemplateId, new SelectList(Model.DisplayList, "TemplateId", "Name"), new { @class = "listviewmodel" }) 

    <p class="editor-label">@Html.LabelFor(model => model.Name)</p> 
    <p class="editor-field">@Html.EditorFor(model => model.Name)</p> 
    <p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p> 
} 

回答

0

當我從帖子動作返回數據時,我意識到了參數轉換異常,因爲我沒有將數據傳遞給SelectItem列表「DisplayList」。這是零,因此砰......

我剛剛填寫丟失的數據來重新顯示,並且錯誤是可見:)赫克......我已經臨危受命,失敗一次......

0

由於服務器端代碼工作正常,我會建議檢查是否需要的JQuery庫加載正確。

<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> 
<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> 
+1

沒有幫助。恰好相反。當我用部分視圖打開jquery對話框時,在輸出窗口中會看到很多異常。 – Elisabeth

+0

如果部分視圖異步加載,則驗證插件需要重新初始化。檢查http://stackoverflow.com/questions/7005052/trouble-getting-unobtrusive-validation-working-with-mvc-3-on-jquery-ajax-post和http://stackoverflow.com/questions/12516245/how -to-check-if-a-div-is-valid-with-unobtrusive-javascript – Sunny

+0

我所有的部分視圖都是異步加載的。除了jquery.validate.unobtrusive.min.js之外,我從不需要任何更進一步的jquery庫。「在我這邊沒有動態內容。 – Elisabeth

0

Insated使用的HTML,BeginForm使用Ajax.BeginForm提交您的局部視圖局部視圖的

//In partial View 


<div id="targetId"> 
    @using (Ajax.BeginForm("Update", "Template", new AjaxOptions { HttpMethod="POST", UpdateTargetId="targetId"})) 
     {  
      @Html.ValidationSummary(false) 
      @Html.DropDownListFor(x => x.TemplateId, new SelectList(Model.DisplayList, "TemplateId", "Name"), new { @class = "listviewmodel" }) 

      <p class="editor-label">@Html.LabelFor(model => model.Name)</p> 
      <p class="editor-field">@Html.EditorFor(model => model.Name)</p> 
      <p class="editor-field">@Html.ValidationMessageFor(model => model.Name)</p> 
     } 

返回類型爲字符串這樣你可以在服務器端捕獲它,在ajax選項中,您必須指定HttpMethod和UpdateTargetId,您希望在視圖中顯示您的結果。我用div引用了您的Ajax.BegionForm,並給出UpdateTargetId是Div Id,因此如果發生錯誤,它會在相同的d中追加視圖四

在控制器

[HttpPost] 
public string Update(EditTemplateListViewModel viewModel) 
{ 
    if (ModelState.IsValid && !_templateDataProvider.TemplateExists(viewModel.Name)) 
    { 
     Template template = Mapper.Map<EditTemplateListViewModel, Template>(viewModel); 
     _templateDataProvider.UpdateTemplate(template); 
     return new JsonNetResult(new { success = true }); 
    } 

    return RenderPartialViewToString("PartialViewName",viewModel); 
} 



protected string RenderPartialViewToString(string viewName, object model) 
     { 
      if (string.IsNullOrEmpty(viewName)) 
       viewName = ControllerContext.RouteData.GetRequiredString("action"); 

      ViewData.Model = model; 

      using (StringWriter sw = new StringWriter()) 
      { 
       ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); 
       ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); 
       viewResult.View.Render(viewContext, sw); 
       return sw.GetStringBuilder().ToString(); 
      } 
     } 

通過傳遞partialViewName和模型RenderPartialViewToStringit將返回觀的RenderHtml

注意:在使用Ajax.BeginForm之前,請確保添加以下腳本。

+0

我已經在使用Ajax。 BeginForm通過使用Jquery Ajax表單發佈Ajax.BeginForm只是MVC的「包裝器」 – Elisabeth

+0

我已經檢查過你的代碼:http://craftycodeblog.com/2010/05/15/asp-net-mvc- render-partial-view-to-string/...我無法解析類Config.Url.ToString()那是什麼配置類? – Elisabeth

+0

Elisa我還沒有得到關於Config.Url.toString()的知識,所以你可以向我提供一些關於它在代碼中使用它的更多細節。 – Shivkumar