通过部分视图MVC3传递模型

时间:2012-11-21 20:21:31

标签: c# asp.net asp.net-mvc-3 partial-views

我有一个像这样的部分视图:

@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,它会正确显示数据。但是,如果我尝试从View内部执行此操作,它会传递当前正在使用的模型,因为它是强类型的。我怎样才能传递我需要的模型(我的成分表列表,db.Ingredientes.ToList());

4 个答案:

答案 0 :(得分:2)

您可以从局部视图引用父视图的模型,因此可能将它们全部打包在顶层视图中。您还可以通过Html.Partial重载明确地(或任何您想要的)传递模型。如果您决定从View访问模型,请确保在View和PartialView中都包含@model指令。

答案 1 :(得分:1)

您的视图期待IEnumerable的模型(NutricionApp.Models.Ingrediente)。您正在传递一个IEnumerable实体(ListaIngredientes)。这就是为什么它在bar。。

假设您的Ingrediente模型的构造函数接受ListaIngredientes作为参数,您可以更改此行:

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

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

应该解决你的问题。

答案 2 :(得分:1)

您是否尝试根据传递给父视图的数据呈现动态部分视图?如果是,请使用Html.RenderAction()方法并对您的控制器进行一些修改,以便每次调用它时返回不同的数据,并将局部视图返回到父视图。 假设Platillo和成分实体与一对多关系相关, 你可以尝试这样的事情:

在父视图中:

@{ 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();

    } 
祝你好运

答案 3 :(得分:0)

我是如何解决的:我刚刚创建了一个新的方法,它包含了我最初使用的方法,以及我想用于PartialView的方法其余的只是使用列表来跟踪我的数据= D < / p>

相关问题