0

我在使用 Html.RenderAction 呈现下拉列表的视图上进行客户端验证时遇到问题。

我有两个控制器。SpecieController 和 CatchController,我已经为我的视图创建了 ViewModel。我想让它尽可能干燥,在不久的将来,我很可能需要一个 DropDownList 用于其他地方的所有 Specie。

当我创建一个 Catch 时,我需要设置与一个物种的关系,我使用从 DropDownList of Species 获得的 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。

查看.捕获.创建

<%@ 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. 客户端验证不适用于 SpecieId(在我重构之后),我明白为什么但不知道如何修复它。
  2. 我可以将 DropDownList SelectedValue“粘合”到 myCatch 中,这样我就不需要从 Request["Species"] 中获取值

在此先感谢您抽出宝贵时间。

4

1 回答 1

0

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

于 2010-11-20T00:21:19.167 回答