1

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; }
2
  • are you sure it will be list, isn't one user will put one offer? Commented Dec 17, 2018 at 10:51
  • an offer has a list of raws and every raw has properies as Part Code, Part Count Part Price and Price of work, So every offer has a client and a list of raws Commented Dec 17, 2018 at 11:18

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much, this helps me a lot. But when i add more than one raw, and than i hit the create button only the first raw is added to the Model List
I have tried to add "asp-for" in the new inputs inside the JS code, but it still adding only the first raw
You could check what data has been passed to controller from F12 Network in Chrome.You need to modify the js code ` var i = ($(".items").length) / 2;` in your real case which represents how many Raw lines there are.
I tried your code everything works fine, so may be the problem is somewhere in my code....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.