简体   繁体   English

通过部分视图MVC3传递模型

[英]Passing a model through a Partial View MVC3

I have a Partial View that goes like this: 我有一个像这样的部分视图:

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

I want to render said partial view in this View, that is strongly typed: 我想在此视图中渲染所述局部视图,这是强类型的:

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

And in my controller I have this: 在我的控制器中我有这个:

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

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

In this case, db.Ingredients.ToList()); 在这种情况下,db.Ingredients.ToList()); returns the data i need to show on the partial view. 返回我需要在局部视图上显示的数据。 Problem is, when I try to display said list in my view, it tells me I have to pass a IEnumerable model corresponding to my view... 问题是,当我尝试在我的视图中显示所述列表时,它告诉我必须传递与我的视图对应的IEnumerable模型...

If I access the PartialView from an URL, it shows me the data correctly. 如果我从URL访问PartialView,它会正确显示数据。 However if i try to do this from inside a View, it passes the Model it's currently using due to being strongly-typed. 但是,如果我尝试从View内部执行此操作,它会传递当前正在使用的模型,因为它是强类型的。 How can I pass the model i need (the list of my ingredients table, db.Ingredientes.ToList()); 我怎样才能传递我需要的模型(我的成分表列表,db.Ingredientes.ToList());

You can reference the parent view's model from the partial view so perhaps package it all together in the top level view. 您可以从局部视图引用父视图的模型,因此可能在顶级视图中将它们全部打包在一起。 You can also pass the model down explicitly (or whatever you want) via the Html.Partial overload. 您还可以通过Html.Partial重载明确地(或任何您想要的)传递模型。 If you decide to access the model from the View make sure you include the @model directive in both View and PartialView. 如果您决定从View访问模型,请确保在View和PartialView中都包含@model指令。

Your view is expecting a IEnumerable of a model (NutricionApp.Models.Ingrediente). 您的视图期待IEnumerable的模型(NutricionApp.Models.Ingrediente)。 You are passing it an IEnumerable of an entity (ListaIngredientes). 您正在传递一个IEnumerable实体(ListaIngredientes)。 That's why it's barfing. 这就是为什么它在bar。

Assuming that the constructor of your Ingrediente model accepts a ListaIngredientes as a parameter, you can change this line: 假设您的Ingrediente模型的构造函数接受ListaIngredientes作为参数,您可以更改此行:

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

to

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

which should fix your problem. 哪个应该解决你的问题。

Are you trying to render dynamic partial view based on data passed to parent view? 您是否尝试根据传递给父视图的数据呈现动态部分视图? if yes, use Html.RenderAction() method and do some modification on your controller so that it returns a different data every time it's called and returns the partial view to parent view. 如果是,请使用Html.RenderAction()方法并对您的控制器进行一些修改,以便每次调用它时返回不同的数据,并将局部视图返回到父视图。 Assuming that there the Platillo and ingredient entities are related with one to many relation, you could try something like this: 假设Platillo和成分实体与一对多关系相关,您可以尝试这样的事情:

In parent view: 在父视图中:

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

Change your controller method: 更改您的控制器方法:

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

    } 

Good luck 祝好运

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

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

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