Home > Software engineering >  I can't send model from view to action .net
I can't send model from view to action .net

Time:08-11

If I simply summarize the work I have done in my code, I have designed a form consisting of many tests and I want to delete the tests marked with checkbox in this form. I mark selected items on my property and check it in controller. I tried to pass the model to the controller as I did before, But this time I can't send the model from the View to the Controller.

In My Razor Code

@model IList<TestModel>
<tbody>
<form >

    <table >
        <tr align="center">
            <td> </td>
            <td>Test No</td>
            <td>Test Adı</td>
            <td>Diğer</td>
        </tr>
        @foreach (var item in Model)
        {
            <tr align="center">
                <input name="id" asp-for="@item.Id" type="hidden" />
                <td><input type="checkbox" asp-for="@item.check" checked="@item.check"></td>
                <td>@(item.Question.Count)</td>
                <td>@item.Name</td>
                <td width="400px">
                    <select  aria-label="Default select example">
                        <option selected>Open this select menu</option>
                        @foreach (var quest in item.Question)
                        {
                            <option value="@quest.Question">@quest.Question</option>
                        }
                    </select>
                </td>
                <td>@*@item.Users*@</td>
            </tr>
        }

        <button type="button"  data-bs-toggle="modal" data-bs-target="#exampleModal">
            Seçili Testleri Sil
        </button>
        <div  id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div >
                <div >
                    <div >
                        <h5  id="exampleModalLabel">Seçili Sorular Silinecek!!</h5>
                        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div >
                        Seçili testleri silmek istediğinizden emin misiniz?
                        <p style="color:red">Bu işlem geri alınamaz!!!</p>
                    </div>
                    <div >
                        <button type="button"  data-bs-dismiss="modal">Vazgeç</button>
                        <button type="submit"  asp-controller="Test" asp-action="DeleteTest" formmethod="post">Seçili Testleri Sil</button>
                    </div>
                </div>
            </div>
        </div>

    </table>
</form>

In My Action Method

 [HttpPost]
    public IActionResult DeleteTest(IList<TestModel> tests) 
    {
        if (tests.Count>0)
        {
        List<TestModel> model = new();
            foreach (var item in tests)
            {
                if (item.check == true)
                {
                    model.Add(item);
                }
            }
                    Context.TestModels.RemoveRange(model);
                    Context.SaveChanges();
        }
        return RedirectToAction("CreateTest");
    }
 

In My Model

 public class TestModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    [InverseProperty(nameof(AppUser.Tests))]
    public AppUser Creater { get; set; }
    [InverseProperty(nameof(AppUser.Exams))]
    public List<AppUser>? Users { get; set; }
    public List<Questions> Question { get; set; }=new List<Questions>();     
    public bool check { get; set; }
    
}

I searched for the error in the code but I can't find it, I would be grateful if you share your advice and help with me.

FormView

FormView2

ControllerImage

CodePudding user response:

1.Model Binding binds the property by name attribute. You need be sure the name attribute match the property name. For the list model, it should be:[index].propertyName.

2.Tag helper asp-for="@item.propertyName" will generate the name="item.propertyName".

3.Your dropdownlist is single select, and the corresponding property is string type named Question in Questions model. But Questions model in main model IList<TestModel> is List<Questions>. So the selected value must always match the first list of the List<Questions>. The name should be:[index].Question[0].propertyName.

4.table element cannot be passed to backend. e.g: @(item.Question.Count) and @item.Name. Only input/select/textarea element can be form submitted.

Change your code like below:

@model IList<TestModel>
<form >

    <table >
        <tr align="center">
            <td> </td>
            <td>Test No</td>
            <td>Test Adı</td>
            <td>Diğer</td>
        </tr>
        @{
            int i = 0;    //add this......
        }
        @foreach (var item in Model)
        {
            <tr align="center">
                     <!--add name attribute-->   
                <input name="[@i].id" asp-for="@item.Id" type="hidden" />
                                            <!--add name attribute-->  
                <td><input type="checkbox" name="[@i].check" asp-for="@item.check" checked="@item.check"></td>
                <td>@(item.Question.Count)</td>
                <td>@item.Name</td>
                <td width="400px">        <!--add name attribute-->  
                <select  name="[@i].Question[0].Question" aria-label="Default select example">
                    <option selected>Open this select menu</option>
                    @foreach (var quest in item.Question)
                    {
                        <option value="@quest.Question">@quest.Question</option>
                    }
                </select>
                </td>
            </tr>
            i  ;   //add this.......
        }

        <button type="button"  data-bs-toggle="modal" data-bs-target="#exampleModal">
            Seçili Testleri Sil
        </button>
        <div  id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div >
                <div >
                    <div >
                        <h5  id="exampleModalLabel">Seçili Sorular Silinecek!!</h5>
                        <button type="button"  data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div >
                        Seçili testleri silmek istediğinizden emin misiniz?
                        <p style="color:red">Bu işlem geri alınamaz!!!</p>
                    </div>
                    <div >
                        <button type="button"  data-bs-dismiss="modal">Vazgeç</button>
                        <button type="submit"  asp-controller="Home" asp-action="DeleteTest" formmethod="post">Seçili Testleri Sil</button>
                    </div>
                </div>
            </div>
        </div>

    </table>
</form>
  • Related