0

I have multiple forms in a razor view. The forms are created dynamically based on database values. Now when I post a form to server I always get empty list but validation is working. Here is my code:

View

<form id="@distinc[i].CategoryName.Replace(" ", string.Empty)" asp-action="Report" asp-controller="Laboratory">

@{
var obj = (List<ReportModel>)ViewBag.ReportModel;
var distinct = obj.DistinctBy(m => m.CategoryName).ToList();

@for (int i = 0; i < distinct.Count(); i++)
{
    <form id="@distinc[i].CategoryName.Replace(" ", string.Empty)" asp-action="Report" asp-controller="Laboratory">

        @{
            var items = obj.Where(m => m.CategoryName == distinc[i].CategoryName).ToList();
        }
        <div class="row g-3">
            @for (int k = 0; k < items.Count(); k++)
            {
                <input asp-for="@items[k].RequestId" hidden />
                <input asp-for="@items[k].ServiceName" hidden />
                <div class="col-md-4 pt-0 pb-0 mb-0 mt-0">
                    <label class="form-label">@items[k].ServiceName</label>
                    <input asp-for="@items[k].Result" class="form-control form-control-sm" />
                    <span asp-validation-for="@items[k].Result" class="small text-danger"></span>
                </div>
            }
        </div>
        <div class="d-flex justify-content-between mt-3 border-0">
            <button type="submit" class="btn btn-sm btn-primary">Save</button>
        </div>
    </form>
}

Actions

[HttpGet]
public async Task<IActionResult> Report(int id)
{
    var model = await laboratoryRepository.GetRequests(id);
    ViewBag.ReportModel = model;
    return View();
}

[HttpPost]
public async Task<IActionResult> Report(List<ReportModel> model)
{
        
}

Model Class

public class ReportModel : PatientInfo
{
    public long RequestId { get; set; }
    public string ServiceName { get; set; }
    public string CategoryName { get; set; }

    [Required]
    public string Result { get; set; }
}

I tried using [FromForm] attribute but no luck. What should I do?

2 Answers 2

1

Make parameters name consistent. Change model to items:

[HttpPost]
public async Task<IActionResult> Report(List<ReportModel> items)
{
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

you cannot post list of model in mvc

[HttpPost]
public async Task<IActionResult> Report(ReportModel model)
{
        
}

Comments

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.