how to get array in action Http Post

Viewed 110

I have a form that is generated from foreach .

part of form :

        @foreach (var Expertise in Model.Expertises)
    {
        <div class="form-check">
            <div class="form-check">
                <input class="form-check-input" type="hidden" asp-for="@Model.Havetreatment.ExpertiseId" value="@Expertise.Id">
                <input class="form-check-input" type="checkbox" asp-for="@Model.Havetreatment.HasExpertise">
                <label class="form-check-label" for="@Expertise.Id">@Expertise.Name</label>
            </div>
        </div>
    }

`
if I wanna get this from action as single class its work but if i wanna get it as param array ,list doest work

work :

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Doctor doctor,Havetreatment havetreatment,Havetreatment havetreatment2,Havetreatment havetreatment3)

does not work :

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Doctor doctor,Havetreatment[] havetreatments)

or :

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Doctor doctor,params Havetreatment[] havetreatments)

or :

        [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(Doctor doctor,list<Havetreatment> havetreatments)

idont wanna use ajax, any help????

1 Answers

I assume you wanna submit a list in asp.net core MVC, and I think you shouldn't create a Havetreatment model and a Expertises model, you should add property HasExpertise to the Expertises model. Then your cshtml should look like this:

@model WebAppMvc1.Models.ViewModelTest

<form method="post">
    @for (int i=0; i<Model.Expertises.Count(); i++)
    {
        <div class="form-check">
            <div class="form-check">
                <input class="form-check-input" type="hidden" asp-for="@Model.Expertises[i].Id" value="@Model.Expertises[i].Id">
                <input class="form-check-input" type="checkbox" asp-for="@Model.Expertises[i].HasExpertise">
                <label class="form-check-label" for="@Model.Expertises[i].Id">@Model.Expertises[i].Name</label>
            </div>
        </div>
    }
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

As you need to submit a list, then you need to add index to each item so they can differ with each other. And in the Controller, you can do like this:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public void Test1(IEnumerable<Expertises> Expertises) {
        var x = HttpContext.Request.Body;
        foreach (var item in Expertises) {
            var a = item.HasExpertise;
        }

    }

View Model:

public class ViewModelTest
    {
        public List<Expertises> Expertises { get; set; }
    }

Expertises model:

public class Expertises
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string HasExpertise { get; set; }
    }

enter image description here

Related