Javorov1103
Javorov1103

Reputation: 139

Populate a List<object> in my Model from the view ASP.NET Core

I have model that has few properties and a List of other objects.

Model Offer:

Car

Date

bla bla

List of OfferRaws

OfferRaw:

PartCode

PartDescription

Price


So in my View when a user makes an offer he has to fill all The Data for the offer, But how i can take the filled info from the user to populate the data for the OfferRaws and they can be added to the List of the Model

 public OfferCreateViewModel()
    {
        this.Raws = new List<OfferRawCreateViewModel>();
    }

    public string CarMake { get; set; }

    public string CarModel { get; set; }

    public string CarRegistrationNumber { get; set; }

    public string CarVinNumber { get; set; }

    public string CarOwner { get; set; }

    public ICollection<OfferRawCreateViewModel> Raws { get; set; }

Upvotes: 1

Views: 1362

Answers (1)

Ryan
Ryan

Reputation: 20116

My understanding is that you would like to add multiple Raws when you create an offer, and then pass all data to your controller.It could be done with the help of js. You could refer to my simple demo below.

1.My Models

public class OfferRawCreateViewModel
{
    public string PartCode { get; set; }
    public string PartDescription { get; set; }
}

public class OfferCreateViewModel
{
    public OfferCreateViewModel()
    {
        this.Raws = new List<OfferRawCreateViewModel>();
    }

    public string CarModel { get; set; }

    public string CarOwner { get; set; }

    public ICollection<OfferRawCreateViewModel> Raws { get; set; }
}

2. View

@model WebApplication1.Models.OfferCreateViewModel


<form asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
    <label asp-for="CarModel" class="control-label"></label>
    <input asp-for="CarModel" class="form-control" />
    <span asp-validation-for="CarModel" class="text-danger"></span>
</div>
<div class="form-group">
    <label asp-for="CarOwner" class="control-label"></label>
    <input asp-for="CarOwner" class="form-control" />
    <span asp-validation-for="CarOwner" class="text-danger"></span>
</div>

<div class="form-group" id="item-list">
    <a href="#" id="add">Add</a>
    <br />
    <input type="text" asp-for="Raws" class="items" name="Raws[0].PartCode" />
    <input type="text" asp-for="Raws" class="items" name="Raws[0].PartDescription" />
</div>
<input type="submit" value="Create" class="btn btn-default" />
</form>
@section Scripts {
<script>
$(function () {

   $("#add").click(function (e) {
       e.preventDefault();
       var i = ($(".items").length) / 2;
       var n = '<br /><input type="text" class="items" name="Raws[' + i + '].PartCode" />' +
           '<input type="text" class="items" name="Raws[' + i + '].PartDescription" />'

       $("#item-list").append(n);
   });

});
</script>
}

3.Controller

 [HttpPost]
 public async Task<IActionResult> Create(OfferCreateViewModel model)

Upvotes: 2

Related Questions