c# - 如何使用 Entity Framework 6 在 ASP.NET MVC 5 中保存自定义模型?

标签 c# asp.net asp.net-mvc entity-framework

设计:

enter image description here

代码:

@using (Html.BeginForm())
{
    ... code block ...

          // BULK INSERT OF COST PER WEIGHT
            @foreach (var item in ViewBag.WeightId)
            {
                <input type="hidden" id="WeightId" name="WeightId" value="@item.Value" />

                <label for="WeightId">@item.Text kg</label>
                @Html.Editor("Cost", new { htmlAttributes = new {  data_weight_id = @item.Value } })
            }

        ... code block ...
}

我没有使用自动生成的模型来保存数据。我正在使用 for 循环生成权重列表和相应的文本框。

我的自定义模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using CouriersHub.DAL;

namespace CouriersHub.Models
{
    public class CostWeight
    {
        [Required]
        [Range(1, 2)]
        public int DomesticOrInternational { get; set; }

        [Required]
        public int CountryId { get; set; }

        [Required]
        public int StateId { get; set; }

        [Required]
        public int CityId { get; set; }

        [Required]
        public int CourierId { get; set; }

        [Required]
        public int ProductId { get; set; }

        [Required]
        public TransportMode TransportMode { get; set; }

        public virtual ICollection<CostWeightList> CostWeightLists { get; set; }
    }

    // TODO: To move to new file
    public class CostWeightList
    {
        public int WeightId { get; set; }
        public float Cost { get; set; }
    }
}

Controller :

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create(
    [Bind(Include = "DomesticOrInternational,CountryId,StateId,CityId,CourierId,ProductId,TransportMode,CostWeightLists")] CostWeight costWeight)
{
    if(ModelState.IsValid)
    {
        // TODO: My Pending action
        await db.SaveChangesAsync();
        return View();
    }
    return View();            
}

POST 数据:

DomesticOrInternational: 1
CountryId: 6
StateId: 4127
CityId: 48403
CourierId: 1
ProductId: 0
TransportMode: 0
CostWeightLists.WeightId: 1
CostWeightLists.Cost: 1
CostWeightLists.WeightId: 2
CostWeightLists.Cost: 2
CostWeightLists.WeightId: 3
CostWeightLists.Cost: 3
CostWeightLists.WeightId: 4
CostWeightLists.Cost: 4
CostWeightLists.WeightId: 5
CostWeightLists.Cost: 5
CostWeightLists.WeightId: 6
CostWeightLists.Cost: 6
CostWeightLists.WeightId: 7
CostWeightLists.Cost: 7
CostWeightLists.WeightId: 8
CostWeightLists.Cost: 8

调试输出:

enter image description here

问题:

如何获取成本和重量并将其循环到 Controller 中?有没有可能把它作为字典发送?

是否可以为这个 View 创建一个模型?如果是,如何创建?

请提供任何想法/建议。

最佳答案

您需要更改 CostWeightLists 的命名以将其绑定(bind)为集合

@using (Html.BeginForm())
{
    ... code block ...

          // BULK INSERT OF COST PER WEIGHT
            @for (var i = 0; i < ViewBag.WeightId.Length; i++)
            {
                <input type="hidden" id="WeightId" name="CostWeightLists[i].WeightId" value="@item.Value" />

                <label for="WeightId">@item.Text kg</label>
                @Html.Editor("Cost", new { htmlAttributes = new {  data_weight_id = @item.Value, name = CostWeightLists[i].Cost} })
            }

        ... code block ...
}

更多信息:ASP.NET MVC 5 View Model Collection Binding

关于c# - 如何使用 Entity Framework 6 在 ASP.NET MVC 5 中保存自定义模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580716/

相关文章:

c# - 多重性与引用约束错误冲突

c# - 无法在 cshtml 中使用内插字符串

c# - 在 C# 中将电影从 Powerpoint 导出到文件

c# - CalendarDatePicker 绑定(bind)空值

asp.net-mvc - Visual Studio 中的单元测试 MVC Web 应用程序和 QTAgent 问题

jQuery Ajax 局部 View 只工作一次

c# - Entity Framework 和抽象类

javascript - 如何隐藏/显示在 ASP.NET 中动态创建的 HTML 元素

asp.net - 尚未使用 Web Api Delegatinghandler 分配内部处理程序

c# - ASP.NET MVC HtmlHelper Razor 语法