繁体   English   中英

通过部分视图MVC3传递模型

[英]Passing a model through a Partial View MVC3

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

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

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

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

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

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

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

哪个应该解决你的问题。

您是否尝试根据传递给父视图的数据呈现动态部分视图? 如果是,请使用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();

    } 

祝好运

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM