0

Html.RenderAction を使用してドロップダウン リストをレンダリングするビューで、クライアント側の検証に問題があります。

私は2つのコントローラーを持っています。SpecieController と CatchController を使用して、ビュー用の ViewModel を作成しました。できるだけDRYに保ちたいので、近い将来、他の場所のすべてのSpecieにDropDownListが必要になるでしょう。

Catch を作成するとき、1 つの種との関係を設定する必要があります。これは、Species の 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 アクションの My View は、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 に対して機能しません (リファクタリングした後)。理由はわかりますが、修正方法がわかりません。
  2. Request["Species"] から値を取得する必要がないように、DropDownList SelectedValue を myCatch に「接着」できますか

お時間を割いていただきありがとうございます。

4

1 に答える 1

0

これが役立つかどうかはわかりませんが、MVC2 のクライアント検証の代わりに xVal を使用しており、説明したシナリオでは問題なく動作します。クライアント側で検証された DropDownLists がたくさんあり、ほとんどが RenderAction でレンダリングされます。

于 2010-11-20T00:21:19.167 に答える