Skip to main content
 首页 » 编程设计

c#之通过局部 View MVC3 传递模型

2024年02月20日12haluo1

我有一个像这样的分部 View :

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

我想在此 View 中呈现所述部分 View ,它是强类型的:

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

在我的 Controller 中我有这个:

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

在这种情况下,db.Ingredients.ToList());返回我需要在局部 View 上显示的数据。问题是,当我尝试在我的 View 中显示所述列表时,它告诉我必须传递与我的 View 对应的 IEnumerable 模型...

如果我从 URL 访问 PartialView,它会正确显示数据。但是,如果我尝试从 View 内部执行此操作,由于是强类型的,它会传递当前正在使用的模型。如何传递我需要的模型(我的配料表列表​​,db.Ingredientes.ToList());

请您参考如下方法:

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