简体   繁体   中英

HttpPost returns null Model in ASP.NET MVC

Advance warning, I am extremely new to ASP.NET.

I'm working on a project which will display rows of data from a db table. When a user clicks the "Ignore" button next to a row, it should update the corresponding "Ignore" column on that row with true in the database.

The view itself works fine, it displays all the data as expected. But when "Ignore" is clicked, and it calls the Ignore() method on the controller, the model is which is passed to the controller is null.

My model, generated by entity framework (with extraneous properties removed):

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace IgnoreDailyItems.Models
{
    [Table("DataChecks.tbl.DailyItems")]
    public partial class DataChecksTblDailyItems
    {
        [Column("entryId")]
        public int EntryId { get; set; }
        [Column("ignore")]
        public bool? Ignore { get; set; }
    }
}

The view:

@model IEnumerable<IgnoreDailyItems.Models.DataChecksTblDailyItems>

@{
    ViewBag.Title = "Placeholder";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EntryId)
        </th>
    </tr>
    
    @{ var item = Model.ToList(); }
    @for(int i = 0; i < Model.Count(); i++)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item[i].EntryId)
            </td>
            <td>
                @using (Html.BeginForm("Ignore", "Home", FormMethod.Post))
                {
                    @Html.HiddenFor(modelItem => item[i].EntryId)
                    <button type="submit" class="btn btn-danger">Ignore</button>
                }
                
            </td>
        </tr>
    }
</table>

And the Ignore() method on the controller:

[HttpPost]
public ActionResult Ignore(DataChecksTblDailyItems modelData)
{
    using (var context = new IgnoreDailyItemsContext())
    {
        var query = context.DataChecksTblDailyItems
            .Where(b => b.EntryId.Equals(modelData.EntryId));

        foreach (var q in query)
        {
            q.Ignore = true;
        }
        context.SaveChanges();
        
        return RedirectToAction("Index", "Home");
    }
}

You need to pass IEnumerable<IEnumerable> as a parameter.

public ActionResult Ignore(IEnumerable<DataChecksTblDailyItems> modelData)
{
  

You're generating the form in wrong way.

@Html.HiddenFor(modelItem => item[i].EntryId)

It will generate an input hidden with item[0].EntryId , item[1].EntryId ... as name/id for each row in the table, for that reason the post model definition does not match.

To solve it, set the input hidden name manually:

@Html.Hidden("EntryId", item[i].EntryId)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM