C# 通过局部视图MVC3传递模型
我有这样一个局部观点:C# 通过局部视图MVC3传递模型,c#,asp.net,asp.net-mvc-3,partial-views,C#,Asp.net,Asp.net Mvc 3,Partial Views,我有这样一个局部观点: @model IEnumerable<NutricionApp.Models.Ingrediente> <table> <tr> <th> NombreIngrediente </th> <th> CantidadPorPorcion </th> <th> UnidadPorPorcion
@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.components.ToList;返回我需要在局部视图上显示的数据。问题是,当我试图在视图中显示所述列表时,它告诉我必须传递一个与视图对应的IEnumerable模型
如果我从URL访问PartialView,它会正确显示数据。但是,如果我尝试从视图内部执行此操作,它会传递当前使用的模型,因为它是强类型的。如何传递模型我需要的成分列表,db.Ingredientes.ToList 您的视图需要一个模型nutricinapp.Models.Ingrediente的IEnumerable。您正在向它传递一个实体ListaIngredientes的IEnumerable。这就是它呕吐的原因 假设IngredEnte模型的构造函数接受ListaIngredEntes作为参数,则可以更改此行:
@Html.Partial("_ListaIngredientes", Model.ListadeIngredientes)
到
这将解决您的问题。您的视图需要一个模型nutricinapp.Models.Ingrediente的IEnumerable。您正在向它传递一个实体ListaIngredientes的IEnumerable。这就是它呕吐的原因 假设IngredEnte模型的构造函数接受ListaIngredEntes作为参数,则可以更改此行:
@Html.Partial("_ListaIngredientes", Model.ListadeIngredientes)
到
这将解决您的问题。您可以从局部视图中引用父视图的模型,因此可以将其打包到顶层视图中。您还可以通过Html.Partial重载显式地向下传递模型,或者传递任何您想要的内容。如果您决定从视图访问模型,请确保在视图和部分视图中都包含@model指令。您可以从部分视图中引用父视图的模型,因此可以将其打包到顶层视图中。您还可以通过Html.Partial重载显式地向下传递模型,或者传递任何您想要的内容。如果您决定从视图访问模型,请确保在View和PartialView中都包含@model指令。是否尝试根据传递给父视图的数据呈现动态部分视图?如果是,请使用Html.RenderAction方法并对控制器进行一些修改,使其在每次调用时返回不同的数据,并将部分视图返回到父视图。 假设Platillo和成分实体与一对多关系相关, 您可以尝试以下方法:
@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>
在父视图中:
@{ 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();
}
祝您好运您是否正在尝试根据传递给父视图的数据渲染动态局部视图?如果是,请使用Html.RenderAction方法并对控制器进行一些修改,使其在每次调用时返回不同的数据,并将部分视图返回到父视图。 假设Platillo和成分实体与一对多关系相关, 您可以尝试以下方法:
@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>
在父视图中:
@{ 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();
}
祝你好运,我是如何解决的:我刚刚创建了一个新方法,它既包含了我最初使用的方法,也包含了我想用于我的部分的方法。其他的只是使用列表来跟踪我的数据=D我是如何解决的:我刚刚创建了一个新方法,它包含了我最初使用的方法,我想用于我的PartialView的方法其余的只是使用列表来跟踪我的数据=D好的。。。问题不在Gredientes的局部视图中,而是在CredientesView中。为了详细说明,我的目标是创建一个动态的partialview,显示成分列表\u ingredients,以便您可以将它们添加到成分列表\u ListaIngredientes.@Angelmenam您正在使用db.ingredients.ToList获取成分。当您从controller加载所有配料时,您如何期望不同的数据?db.contracents.Tolist为我提供了数据库中每个配料的列表。但是ListaDeComponents有它自己的ingredients对象,我用它来复制和粘贴db.contracents.Tolist项中的信息。好的。。。问题不在Gredientes的局部视图中,而是在CredientesView中。为了详细说明,我的目标是创建一个动态的partialview,显示成分列表\u ingredients,以便您可以将它们添加到成分列表\u ListaIngredientes.@Angelmenam您正在使用db.ingredients.ToList获取成分。当您从controller加载所有配料时,您如何期望不同的数据?db.contracents.Tolist为我提供了数据库中每个配料的列表。但是ListaDeComponents有它自己的ingredients对象,我用它来复制和粘贴db.Components.Tolist项中的信息。我不想要父级的模型,因为我想引用另一个。我不想要父级的模型,因为我想引用另一个。你需要发布错误的文本。你需要发布文本 错误的原因。