2012-01-24 36 views
1

我希望我解释正确.. 我想要做的是建立一个会议阵列与产品列表中。 然后显示这些文本中的窗体分位旁边的分栏,并能够提交它们。我想我需要使用模板编辑器。但我不知道如何将数据放入项目列表中。MVC模板编辑器如何显示来自两个模型的项目

这是怎么了,目前正在填充我的会话变量..

IList<EnqProduct> items2 = Session["enquiry"] as IList<EnqProduct>; 
desc = desc.Replace(",", ""); 
EnqProduct item = new EnqProduct(); 
item.Id = (items2.Count + 1).ToString(); 
item.Product = desc; 
item.Quantity = "0"; 
items2.Add(item); 

所以递减,可以productone,产品分类二等等

询问产品型号:

namespace MvcEditorTemplates.Models 
{ 
    public class EnqProduct 
    { 
     public string Id { get; set; } 
     public string Product { get; set; } 
     public string Quantity { get; set; } 
    }  
} 

正常询价型号:

public class Enquiry 
{ 
    public List<EnqProduct> EnqProduct { get; set; } 
} 

我如何试图填充​​模型,但这是静态的。我需要从数组项填充:

var EnquiryModel = new Enquiry { 
EnqProduct = items2.Select(c => new EnqProduct() 
{ 
     Quantity = c.Quantity, 
     Product = c.Product 
})  
}; 

询问产品模板视图:

@model MvcEditorTemplates.Models.EnqProduct 
<div class="fl"> 
    <p> 
     @Html.LabelFor(x => x.Product) 
     @Html.TextBoxFor(x => x.Product) 
    </p> 
    <p> 
     @Html.LabelFor(x => x.Quantity) 
     @Html.TextBoxFor(x => x.Quantity) 
    </p> 
</div> 

这是怎么即时试图让它显示喧嚣的观点:

@Html.EditorFor(model => model.EnqProduct) 

编辑:

items2.Select(c => new EnqProduct()

我得到一个IEnumerbale错误一些关于转换?

+0

如果要显示来自两个模型的数据,只需创建具有两个属性的组合ViewModel;每个型号一个。 –

+0

我需要将它提交回aswel,并且产品列表是动态的 – Beginner

回答

1

尝试这样:

public class ErrorMessage 
{ 
    public DateTime ErrorDate { get; set; } 
    public string ErrorText { get; set; } 
    public int DexRowId { get; set; } 
} 

public class Transaction 
{ 
    public string TransactionType { get; set; } 
    public string Processed { get; set; } 
    public DateTime UpdateDate { get; set; } 
    public int DexRowID { get; set; } 
    public string Text { get; set; } 
} 
public class Result 
{ 
    public List<ErrorMessage> errorMessageList { get; set; } 
    public List<Transaction> transactionList { get; set; } 

} 

在你的控制器:

List<Transaction> transactionList = ...;//query to populate your list; 
List<ErrorMessage> errorMessageList = ...;//query to populate your list; 

Result result = new Result(); 
result.ErrorMessageList = errorMessageList; 
result.TransactionList = transactionList; 

return View(result); 

,并在您的视图:

@model Models.Result 
@{ 
    ViewBag.Title = "Result"; 
    Layout = "~/Views/Shared/_ResultLayout.cshtml"; 
} 

编辑:

@model IENumerable<MvcEditorTemplates.Models.EnqProduct> 
@{ 
foreach(EnqProduct ep in @model) 
{ 
    .... your code comes here......... 
} 
} 
+0

我认为这与我正在尝试执行的操作类似,即时尝试通过我的查询模型查看产品列表,但是如何填充每个不同的一个 – Beginner

+0

如果您的视图接受@ model MvcEditorTemplates.Models.EnqProduct的列表,而不是您应该使用@ model IENumerable ,或者您可以在模型中传递EnqProduct。你可以使用foreach循环显示列表,参见我的编辑 – user1157131