1

次のような部分ビューがあります。

@model IEnumerable<NutricionApp.Models.Ingrediente>

<table>
<tr>
    <th>
        NombreIngrediente
    </th>
    <th>
        CantidadPorPorcion
    </th>
    <th>
        UnidadPorPorcion
    </th>
    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.NombreIngrediente)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.CantidadPorPorcion)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.UnidadPorPorcion)
    </td>

</tr>
}

</table>

このビューで、強く型付けされた部分ビューをレンダリングしたいと思います。

@model NutricionApp.Models.Platillo

@{
    ViewBag.Title = "Create";
    Model.ListadeIngredientes = new List<NutricionApp.Models.ListaIngredientes>();
}

<h2>Create</h2>

<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>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Platillo</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.NombrePlatillo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.NombrePlatillo)
            @Html.ValidationMessageFor(model => model.NombrePlatillo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.idRestaurante)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.idRestaurante)
            @Html.ValidationMessageFor(model => model.idRestaurante)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.DescripcionPlatillo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.DescripcionPlatillo)
            @Html.ValidationMessageFor(model => model.DescripcionPlatillo)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.esAprobado)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.esAprobado)
            @Html.ValidationMessageFor(model => model.esAprobado)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.esDisponible)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.esDisponible)
            @Html.ValidationMessageFor(model => model.esDisponible)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.precio)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.precio)
            @Html.ValidationMessageFor(model => model.precio)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.VigenciaPlatillo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.VigenciaPlatillo)
            @Html.ValidationMessageFor(model => model.VigenciaPlatillo)
        </div>
        <!--<table>
        <tr><th>Nombre</th></tr>
        @@foreach (var item in (List<NutricionApp.Models.ListaIngredientes>)Session["Lista"])
                {
            <tr>
            <p>
                <td>@@item.ingrediente.NombreIngrediente</td>
            </p>
            </tr>
        }
        </table>-->
        @Html.Partial("_Ingredientes", Model.);
        <br />
        @Html.Partial("_ListaIngredientes", Model.ListadeIngredientes)

        <p>
            <input type="submit" value="Create" />
        </p>

    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

そして、私のコントローラーにはこれがあります:

//....
 public ActionResult _ListaIngredientes()
    {
        IEnumerable<ListaIngredientes> ListaDemo = new List<ListaIngredientes>();
        return View(ListaDemo);
    }

    public ActionResult _Ingredientes()
    {
        return View(db.Ingredientes.ToList());
    } 

この場合、db.Ingredients.ToList()); 部分ビューに表示する必要があるデータを返します。問題は、ビューに上記のリストを表示しようとすると、ビューに対応する IEnumerable モデルを渡す必要があることがわかります...

URL から PartialView にアクセスすると、データが正しく表示されます。ただし、ビュー内からこれを実行しようとすると、厳密に型指定されているため、現在使用しているモデルが渡されます。必要なモデル (食材テーブルのリスト、db.Ingredientes.ToList()) を渡すにはどうすればよいですか?

4

4 に答える 4

2

部分ビューから親ビューのモデルを参照できるので、トップ レベル ビューにまとめてパッケージ化することができます。Html.Partial オーバーロードを介してモデルを明示的に (または必要に応じて) 渡すこともできます。ビューからモデルにアクセスする場合は、ビューと PartialView の両方に @model ディレクティブを含めるようにしてください。

于 2012-11-21T20:42:37.667 に答える
1

ビューは、モデル (NutricionApp.Models.Ingrediente) の IEnumerable を期待しています。エンティティ (ListaIngredientes) の IEnumerable を渡しています。それがバーフィングである理由です。

Ingrediente モデルのコンストラクターが ListaIngredientes をパラメーターとして受け入れると仮定すると、次の行を変更できます。

@Html.Partial("_ListaIngredientes", Model.ListadeIngredientes)

@Html.Partial("_ListaIngredientes", Model.ListadeIngredientes.Select(i => new Ingrediente(i)))

これで問題が解決するはずです。

于 2012-11-21T20:35:44.497 に答える
1

親ビューに渡されたデータに基づいて動的部分ビューをレンダリングしようとしていますか? はいの場合は、Html.RenderAction() メソッドを使用し、コントローラーを変更して、呼び出されるたびに異なるデータを返し、部分ビューを親ビューに返すようにします。Platillo と成分エンティティが 1 対多の関係で関連付けられていると仮定すると、次のようなことを試すことができます。

親ビュー:

@{ Html.RenderAction("_Ingredientes", "YourControllerName", new {platilloId=Model.PlatilloId}); }

コントローラーの方法を変更します。

public ActionResult _Ingredientes(int platilloId )
    {
return PartialView( "_IngredientView", db.Platillos.where(p=>p.PlatilloId==platilloId ).Ingredients.ToList();

    } 

幸運を

于 2012-11-21T23:24:27.777 に答える
0

解決方法:最初に使用していたメソッドと、PartialViewに使用したいメソッドの両方を含む新しいメソッドを作成しました。残りは、データを追跡するためにリストを使用するだけでした= D

于 2012-11-22T07:20:34.763 に答える