c# - 如何使用 Entity Framework 6 在 ASP.NET MVC 5 中保存自定义模型?
问题描述
设计:
代码:
@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; }
}
}
控制器:
[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
调试输出:
问题:
我怎样才能把成本和重量循环到控制器中?有没有可能将它作为字典发送?
是否可以为此视图创建模型?如果是,如何创建?
请提供任何想法/建议。
解决方案
您需要更改CostWeightLists
视图的命名以将其绑定为集合
@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 ...
}
推荐阅读
- sql - 使用最大值 SQL 更新组中的所有行
- c++ - Visual Studio C++ 链接器无法打开库文件
- javascript - 页面更改时未加载外部脚本
- azure - azure 上生产的 Blazor 服务器:工作一段时间然后突然在所有 css/脚本文件上出现 404?
- flutter - 如何清除flutter web中的堆内存?
- javascript - 当命名函数位于匿名函数中时,为什么“this”关键字不起作用?
- javascript - 如何在javascript中将第一个输出转换为第二个输出?
- python - 使用 groupby 时 pandas 数据框的访问键
- amcharts - 检索 amchart4 treeMap 的叶子(框)的名称
- python - Apache 气流可以在 Windows 上使用吗?应该提前做什么?