1

Using Razor code, how can I bind a list of Widgets to the List<WidgetModel> in my Model? I assume there's an easy standard way to do this as it seems like a common scenario.

I have a model with a list of widgets, like this:

public class CustomerModel
{
    public List<WidgetsModel> widgets { get; set; }
}

I also have a View where the user can add widgets

<input type="button" id="add-link" value="Add"/>

<div class="widget-wrapper">

    <p>Widget Name:</p>
    @Html.TextBoxFor(m => m.widgets.name)

    <p>Widget Price:</p>
    @Html.TextBoxFor(m => m.widgets.price)
  
    <hr>
</div>

<script type="text/javascript">
    $('#add-link').on('click', function(){
        var x = $('.widget-wrapper')[0].outerHTML;
        $(x).insertAfter(this);
    });
</script>
Yiyi You
  • 16,875
  • 1
  • 10
  • 22
KidBilly
  • 3,408
  • 1
  • 26
  • 40

2 Answers2

3

Here is a working demo.When click the add button,it will creat a input for name and a input for price.If fill in the inputs,and click the submit button,it will post the whole data to action.

Models:

public class CustomerModel
    {
        public List<WidgetsModel> widgets { get; set; }
    }
    public class WidgetsModel {
        public string name { get; set; }
        public int price { get; set; }

    }

View:

<form method="post">
    <input type="button" id="add-link" value="Add" />
    <div class="widget-wrapper">
        @for (var i = 0; i < Model.widgets.Count(); i++)
        {


            <p>Widget Name:</p>
            @Html.TextBoxFor(m => m.widgets[i].name)

            <p>Widget Price:</p>
            @Html.TextBoxFor(m => m.widgets[i].price)

            <hr>


        }

    </div>
    <input type="submit" id="add-link" value="Submit" />
</form>
<script type="text/javascript">
    
    $('#add-link').on('click', function () {
        var count = $('input[name*="name"]').length;
        var html = '<p>Widget Name:</p><input id = "widgets_' + count + 'name" name = "widgets[' + count + '].name" type = "text" ><p> Widget Price:</p> <input id="widgets_' + count + '__price" name="widgets[' + count + '].price" type="text" ><hr>';
        $('.widget-wrapper').append(html);
    });
</script>

Controller:

[HttpGet]
        public IActionResult TestCustomerModel() {
            CustomerModel c = new CustomerModel { widgets = new List<WidgetsModel> { new WidgetsModel { name = "w1", price = 1 }, new WidgetsModel { name = "w2", price = 2 }, new WidgetsModel { name = "w3", price = 3 } } };
            return View(c);
        }
        [HttpPost]
        public IActionResult TestCustomerModel(CustomerModel c)
        {
           
            return Ok();
        }

result: enter image description here

Yiyi You
  • 16,875
  • 1
  • 10
  • 22
  • Yiyi, there's something I don't understand in the HTML snippet you shared above. You have a button and a submit button, both named add-link. Won't that cause problems? – Rod Aug 18 '22 at 19:04
-2

Could you elaborate on the question. What exactly do you want to do. Do you want to display the list of widgets to a view?

You could refer this thread. It can solve your issue IOException: The process cannot access the file 'file path' because it is being used by another process

Ajinkya Gadgil
  • 117
  • 1
  • 8