2010-06-01 71 views
0

我在用Html.RenderAction帮助呈现下拉列表的View上遇到客户端验证问题。MVC2客户端验证与RenderAction视图中的注释

我有两个控制器。 SpecieController和CatchController,我为我的视图创建了ViewModels。 我想尽可能保持干燥,我很可能需要在不久的将来在其他地方的所有物种的DropDownList。

当我创建一个Catch时,我需要将一个关系设置为一个物种,我使用从物种的DropDownList中获得的一个ID来执行此操作。

ViewModels.Catch.Create

[Required] 
public int Length { get; set; } 

[Required] 
public int Weight { get; set; } 

[Required] 
[Range(1, int.MaxValue)] 
public int SpecieId { get; set; } 

ViewModels.Specie.DropDownList

public DropDownList(IEnumerable<SelectListItem> species) { this.Species = species; } 
public IEnumerable<SelectListItem> Species { get; private set; } 

我对Catch.Create动作视图使用ViewModels.Catch.Create作为模型。

但感觉我在实现中缺少了一些东西。我想要的是将来自RenderAction的DropDownList中的选定值连接到我的SpecieId。

View.Catch.Create

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<BC.ProjectName.Web.ViewModels.CatchModels.Create>" %> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 

    <% Html.EnableClientValidation(); %> 
    <% using (Html.BeginForm()) {%> 
     <%: Html.ValidationSummary(true) %> 

     <fieldset> 
      <div class="row"> 
       <div class="editor-label"> 
        <%: Html.LabelFor(model => model.Weight) %> 
       </div> 
       <div class="editor-field"> 
        <%: Html.TextBoxFor(model => model.Weight) %> 
        <%: Html.ValidationMessageFor(model => model.Weight) %> 
       </div> 
      </div> 

      <div class="row"> 
       <div class="editor-label"> 
        <%: Html.LabelFor(model => model.Length) %> 
       </div> 
       <div class="editor-field"> 
        <%: Html.TextBoxFor(model => model.Length) %> 
        <%: Html.ValidationMessageFor(model => model.Length) %> 
       </div> 
      </div> 

      <div class="row"> 
       <div class="editor-label"> 
        <%: Html.LabelFor(model => model.SpecieId) %> 
       </div> 
       <div class="editor-field"> 
        <%-- Before DRY refactoring, works like I want but not DRY 
         <%: Html.DropDownListFor(model => model.SpecieId, Model.Species) %> 
        --%> 
        <% Html.RenderAction("DropDownList", "Specie"); %> 
        <%: Html.ValidationMessageFor(model => model.SpecieId) %> 
       </div> 
      </div> 

      <div class="clear"></div> 

      <input type="submit" value="Save" /> 

     </fieldset> 

    <% } %> 

</asp:Content> 

CatchController.Create

[HttpPost] 
public ActionResult Create(ViewModels.CatchModels.Create myCatch) 
{ 
    if (ModelState.IsValid) 
    { 
     // Can we make this StronglyTyped? 
     int specieId = int.Parse(Request["Species"]); 

     // Save to db 
     Catch newCatch = new Catch(); 
     newCatch.Length = myCatch.Length; 
     newCatch.Weight = myCatch.Weight; 
     newCatch.Specie = SpecieService.GetById(specieId); 
     newCatch.User = UserService.GetUserByUsername(User.Identity.Name); 

     CatchService.Save(newCatch); 

     // After save redirect 
     return Redirect("/"); 
    } 

    // Invalid 
    return View(); 
} 

这个场景的作品,但并不顺利,因为我想要的。

  1. ClientSide验证不适用于SpecieId(我重构后),我明白为什么,但不知道如何才能ix它。
  2. 我能“胶水”将DropDownList的SelectedValue到myCatch所以我并不需要从请求[“种”]获得价值

预先感谢你的时间在这。

+0

请问您可以查看您的视图和创建操作的整个代码吗? 客户端验证通常与以下html助手一起应用:'Html.EnableClientSideValidation()' – 2010-06-01 23:03:05

+0

整个代码,粘贴。 ClientValidation适用于其他字段,长度和重量。在重构RenderAction之前,它也用于下拉菜单。 Species集合然后被放置在Catch.Create对象的旁边,长度,重量和SpecieId道具。 – Olle 2010-06-02 09:24:16

回答

0

我不确定这是否有帮助,但我使用的是xVal而不是MVC2的客户端验证,并且在您描述的场景下它工作正常。我有很多客户端验证的DropDownLists,我主要使用RenderAction渲染它们。