【问题标题】:Passing a model through a Partial View MVC3通过局部视图 MVC3 传递模型
【发布时间】:2012-11-21 20:21:31
【问题描述】:

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

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

【问题讨论】:

  • 你需要发布错误的文本。

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


【解决方案1】:

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

【讨论】:

  • 我不想要父母的模型,因为我想引用另一个
【解决方案2】:

您的视图需要一个模型的 IEnumerable (NutricionApp.Models.Ingrediente)。您正在向它传递一个实体 (ListaIngredientes) 的 IEnumerable。这就是它在呕吐的原因。

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

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

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

这应该可以解决您的问题。

【讨论】:

  • 好的...问题不在 _ListaIngredientes 部分视图中,而是在 _IngredientesView 中。详细地说,我的目标是创建一个显示成分列表 (_Ingredientes) 的动态局部视图,以便您可以将它们添加到成分列表 (_ListaIngredientes)。
  • @Angelmenam 您正在使用 db.Ingredientes.ToList() 来获取成分。当您从控制器加载所有成分时,您如何期望不同的数据?
  • db.ingredients.Tolist() 为我提供了数据库中每种成分的列表。但是 ListaDeIngredients 有它自己的成分对象,我用它来复制和粘贴来自 db.ingredients.Tolist() 项目的信息。
【解决方案3】:

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

    } 

祝你好运

【讨论】:

    【解决方案4】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-19
      • 2019-02-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 2014-02-05
      相关资源
      最近更新 更多