简体   繁体   English

将数据从视图传递到 model 中的 model

[英]Passing data from view to model in model

I'm reading data from my view.我正在从我的视图中读取数据。 And I need to pass data to controller via model.我需要通过 model 将数据传递给 controller。 But the problem is that I need to pass data to model in model.但问题是我需要将数据传递给model中的model。

I've tried this one我试过这个

@foreach (var item in Model.Items)
{
    @Html.TextBoxFor(item.ItemCount, null, new { @class="input_quantity- 
    value", value = "2.5", data_type="area", data_width="2.5"})
}

but this is incorrect但这是不正确的

Here's my code for view这是我的查看代码

@model TechnoTent.Models.ViewModel.OrderVM

@using (Html.BeginForm("EditOrder", "AdminOrder", FormMethod.Post, new { 
@class = "product-edit", enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @foreach (var item in Model.Items)
    {
        @Html.TextBoxFor(item.ItemCount, null, new { 
        @class="input_quantity-value", value = "2.5", data_type="area", data_width="2.5"})
    }
}

Here code for controller这里代码为 controller

[HttpPost]
public ActionResult EditOrder(OrderVM order)
{
    AdminOrders.EditOrder(order);

    return View();
 }

and here's a part of code for my model这是我的 model 的一部分代码

 public class OrderVM
 {
     public List<OrderItemsVM> Items { get; set; }
 }

and here's a part of code for my OrderItemsVM model这是我的 OrderItemsVM model 的一部分代码

 public class OrderItemsVM
 {
      public string ItemCount { get; set; }
 }

I need to read ItemCount from a view to my OrderItemsVm.我需要从视图中读取 ItemCount 到我的 OrderItemsVm。 Is it possible somehow?有可能吗? or better to have a List in a base model and to read it there?或者更好地在基础 model 中有一个列表并在那里阅读?

Your controller and view models look fine to me.您的 controller 和查看模型对我来说看起来不错。 This is how I would do the views.这就是我将如何做的意见。 First add a folder "EditorTemplates" inside the Views folder.首先在 Views 文件夹中添加一个文件夹“EditorTemplates”。 Then create OrderItemsVM.cshtml view inside that folder.然后在该文件夹中创建 OrderItemsVM.cshtml 视图。

/EditorTemplates/OrderItemsVM.cshtml: /EditorTemplates/OrderItemsVM.cshtml:

@model TechnoTent.Models.ViewModel.OrderItemsVM

<div>
    @Html.TextBoxFor(model => model.ItemCount, new { @class = "..." })
</div>

In your form page, get rid of @foreach(...){...} block and replace it with @Html.EditorFor(model => model.Items)在您的表单页面中,摆脱 @foreach(...){...} 块并将其替换为 @Html.EditorFor(model => model.Items)

YourFormPage.cshtml: YourFormPage.cshtml:

@model TechnoTent.Models.ViewModel.OrderVM

@using (Html.BeginForm(...))
{
    @Html.AntiForgeryToken()

    @Html.EditorFor(model => model.Items)
    ...
}

Please change below code in views请在视图中更改以下代码

cshtml page cshtml页面

    @model TechnoTent.Models.ViewModel.OrderVM

    @using (Html.BeginForm("EditOrder", "AdminOrder", FormMethod.Post, new { 
    @class = "product-edit", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()        
        for(int i=0; i < Model.Items.Count;i++)
        {
@Html.TextBox("Items["+i+"].ItemCount", "2.5", new { 
            @class="input_quantity-value", value = "2.5", data_type="area", data_width="2.5"})
        }
    }

cshtml page if you want set model values in controller like you are editing the data cshtml 页面,如果您想在 controller 中设置 model 值,就像您正在编辑数据一样

    @model TechnoTent.Models.ViewModel.OrderVM

    @using (Html.BeginForm("EditOrder", "AdminOrder", FormMethod.Post, new { 
    @class = "product-edit", enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()        
        for(int i=0; i < Model.Items.Count;i++)
        {
@Html.TextBox("Items["+i+"].ItemCount",Model.Items[i].ItemCount , new { 
            @class="input_quantity-value", value = "2.5", data_type="area", data_width="2.5"})
        }
    }

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

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