MVC模板编辑器如何显示来自两个模型的项

本文关键字:两个 模型 显示 编辑器 何显示 MVC | 更新日期: 2023-09-27 18:21:21

我希望我能正确解释这一点。。我正在尝试建立一个会话数组,其中包含一个产品列表。然后在表单上的文本框中显示这些内容,旁边有分位数,并且可以提交这些内容。我想我需要使用模板编辑器。但我不知道如何将数据放入项目列表中。

这就是当前填充会话变量的方式。。

 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);

所以desc,可以是产品一、产品二等

查询产品型号:

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错误关于cast的什么?

MVC模板编辑器如何显示来自两个模型的项

试试这样的东西:

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.........
}
}