gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:28:05 25 4
gpt4 key购买 nike

设计:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com