ASP.Net Core MVC: Bind Model inside View

benuklemi
 
on Mar 06, 2022 07:36 PM
Sample_129024.zip
1104 Views

I Am trying to create a form to collect the qualifications of students. at first, the list should contain 4 objects for four different qualifications.

//the segment of the *create view*
@for (var i = 0; i < 4; i++)
{
    <div class="col-md-3 me-2">
        <label asp-for="@Model[i].Institute" class="control-label col-md"></label>
        <input asp-for="@Model[i].Institute" class="form-control" />
        <span asp-validation-for="@Model[i].Institute" class="text-danger"></span>
    </div>

    <div class="col-md-3 me-2">
        <label asp-for="@Model[i].YearGraduated" class="control-label"></label>
        <input asp-for="@Model[i].YearGraduated" class="form-control" />
        <span asp-validation-for="@Model[i].YearGraduated" class="text-danger"></span>
    </div>
}
<div class="form-group d-flex-row m-auto mt-2">
    <input type="submit" name="submit" value="Save and Continue" />
</div>

 

public IActionResult Create()
{
    var model = new List<Qualification>();
    model = _context.Qualifications.ToList();
    return View(model);
}

 My problem is when I run the code, nothing is generated and no error at all.

Can anyone help

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Mar 07, 2022 01:27 AM

Hi benuklemi,

Check this example. Now please take its reference and correct your code.

Model

public class PersonModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        List<PersonModel> persons = new List<PersonModel>();
        persons.Add(new PersonModel { Id = 1, Name = "John Hammond", Country = "United States" });
        persons.Add(new PersonModel { Id = 2, Name = "Mudassar Khan", Country = "India" });
        return View(persons);
    }
}

View

@model List<List_Model_Core_MVC.Models.PersonModel>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">
        @for (int i = 0; i < Model.Count(); i++)
        {
            <div class="row">
                <div class="col-md-3 me-2">
                    <label asp-for="@Model[i].Id" class="control-label col-md"></label>
                    <input asp-for="@Model[i].Id" class="form-control" />
                    <span asp-validation-for="@Model[i].Id" class="text-danger"></span>
                </div>

                <div class="col-md-3 me-2">
                    <label asp-for="@Model[i].Name" class="control-label"></label>
                    <input asp-for="@Model[i].Name" class="form-control" />
                    <span asp-validation-for="@Model[i].Name" class="text-danger"></span>
                </div>

                <div class="col-md-3 me-2">
                    <label asp-for="@Model[i].Country" class="control-label"></label>
                    <input asp-for="@Model[i].Country" class="form-control" />
                    <span asp-validation-for="@Model[i].Country" class="text-danger"></span>
                </div>
            </div>
        }
    </div>
</body>
</html>

Screenshot