简体   繁体   English

从MVC3中的模型列表创建局部视图

[英]Creating a Partial View from a Model's list in MVC3

So, i have this class here: 所以,我这里有这个课:

public class Platillo
{
    public virtual int ID { get; set; }
    public virtual String NombrePlatillo { get; set; }
    public virtual int idRestaurante { get; set; }
    public virtual String DescripcionPlatillo { get; set; }
    public virtual bool esAprobado { get; set; }
    public virtual bool esDisponible { get; set; }
    public virtual double precio { get; set; }
    public virtual DateTime vigenciaPlatillo { get; set; }
    public virtual List<ListaIngredientes>  listadeIngredientes { get; set;}
}

I've created a strongly typed PlatilloController that makes all basic CRUD operations. 我创建了一个强类型的PlatilloController,它可以进行所有基本的CRUD操作。 The problem is, the View renderes everything but the List. 问题是,视图会渲染列表以外的所有内容。 My idea is to create a List that allows to add new ingredients(Ingredientes) and the amount of servings (Porciones) for each one on a dish (Platillo). 我的想法是创建一个列表,该列表允许为一道菜(Platillo)中的每一种添加新成分(成分)和份量(羊乳干酪)。

public class ListaIngredientes
{
    public virtual int ID { get; set; }
    public virtual Ingrediente ingrediente { get; set; }
    public virtual int porciones { get; set; }
}

public class Ingrediente
{
    public virtual int ID { get; set; }
    public virtual String NombreIngrediente { get; set; }
    //...
}

So, what i was thinking was to implement a PartialView that rendered a list of every ingredient on the dish, and that allowed to add new ingredients. 因此,我当时想实现一个PartialView,该PartialView呈现了碟中每种成分的清单,并允许添加新的成分。 How exactly do I do that? 我该怎么做? And, since i plan on creating and updating new ingredients on the same page, i'm sure i should be taking AJAX into account. 而且,由于我计划在同一页面上创建和更新新的成分,因此我确定应该考虑AJAX。 How exactly can i use Ajax to Create and display ListaIngrediente's list entries? 我到底如何使用Ajax创建和显示ListaIngrediente的列表条目?

Just a sample using a partial view page, try this: 只是使用部分视图页面的示例,请尝试以下操作:

@model <Project>.Models.Platillo

@foreach(ListaIngredientes ing in listadeIngredientes)
{
      <div>@ing.ID</div>
      <div>@ing.Ingrediente.NombreIngrediente</div>
}

I am alittle bit confused about your design why would you use a list public 我对您的设计有点困惑,为什么要使用公共列表

virtual List<ListaIngredientes> listadeIngredientes { get; set;} virtual List<ListaIngredientes> listadeIngredientes { get; set;} I would start but first creating a Platillo foreign key in your ListaIngredientes that would match with Platillo something like PlatilloID so now you would have a list of ListaIngredientes that match the Platilloios and it ingridients. virtual List<ListaIngredientes> listadeIngredientes { get; set;}我将开始但首先在您的ListaIngredientes中创建一个Platillo外键,该外键将与Platillo匹配,例如PlatilloID,因此现在您将拥有一个与Platilloios及其内在元素匹配的ListaIngredientes列表。

and then sure you can create an ajax partial view at which you can add Ingrediente and then match specific ingridients with your Platilloio on a separate view in another word create separately ingridiets and Platilloio and then on another view add ingridiets from the ingridients in your Ingridients table to Pelatio. 然后确保可以创建一个ajax部分视图,在该视图中可以添加成分,然后在另一个视图中的另一个单词上将特定成分与Platilloio匹配,换句话说,分别创建成分和Platilloio,然后在另一个视图上,从Ingridients表中的成分中添加成分去Pelatio。

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

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