[英]How to Display IEnumerable<IGrouping<string, Model>> with an editor template
[英]MVC template editor how to display items from two model
我希望我能正確解釋。我想做的是建立一個包含產品列表的會話數組。然后將它們顯示在表格中的文本框中,旁邊有分位數,然后可以提交它們。 我想我需要使用模板編輯器。 但是我不知道如何將數據放入項目列表。
這是當前填充我的會話變量的方式。
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,product 2等等。
查詢產品型號:
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錯誤,有關投放?
嘗試這樣的事情:
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.........
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.