Aravin
Aravin

Reputation: 7067

How to save custom model in ASP.NET MVC 5 with Entity Framework 6?

Design:

enter image description here

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.

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:

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:

enter image description here

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.

Upvotes: 3

Views: 820

Answers (1)

Mohsen Esmailpour
Mohsen Esmailpour

Reputation: 11544

you need to change naming of CostWeightLists in view to bind it as collection

@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

Upvotes: 1

Related Questions