[英]How to save custom model in ASP.NET MVC 5 with Entity Framework 6?
Design: 设计:
Code: 码:
@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 ...
}
I am not using auto-generated model to save data. 我没有使用自动生成的模型来保存数据。 I am using a for loop to generate the weight list and the respective textboxes. 我正在使用for循环来生成权重列表和相应的文本框。
My custom model: 我的定制型号:
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
data: 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
Debug Output: 调试输出:
Question : 问题 :
how can I take the cost & weight and loop it into the controller? 如何获取成本和重量并将其循环到控制器中? Is there any possibility to sent it as a dictionary? 有没有可能把它作为字典发送?
Is it possible to create a model for this view? 是否可以为此视图创建模型? If yes, how to create? 如果是,如何创建?
Please provide any idea/suggestions. 请提供任何想法/建议。
you need to change naming of CostWeightLists
in view to bind it as collection 您需要更改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 ...
}
more info: ASP.NET MVC 5 View Model Collection Binding 更多信息: ASP.NET MVC 5查看模型集合绑定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.