为什么模型不在 post 传回控制器?

Why does the model not pass back to the controller on post?

我的模型没有在 post 传回控制器。我在应用程序的另一部分执行此操作并且工作正常。我可能错过了一些东西,但我已经过了两天了,但我看不到它。希望大家多多帮助。

我的看法:

@model Towins.Models.T_Complaints
@{
  ViewBag.Title = "UpsertComplaint";
  Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("UpsertComplaint_Post", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()

// The model is working on the get. 
// The date has been supplied in the get and I have it in the view. 
// The Model.Id == 0 as this is the add portion of the upsert. 
// Can't even start working on the update portion as the model is not coming back at all.

<h4>@(Model.Id!=0 ? "Edit" : "Add") Complaint</h4>
<hr />
@Html.HiddenFor(model => model.Id)
@Html.HiddenFor(model => model.WreckerID)
<div class="container text" style="text-align: center">
    <div class="form-row">
        <div class="form-group col-6">
            <label for="NewWrecker" class="col-form-label">Wrecker Company Name</label>
            <input type="text" disabled class="form-control" id="WreckerCo" name="WreckerCo" value="@ViewBag.WreckerCo" />
        </div>
    </div>
    <div class="form-row">
        <div id="DateOfComplaint-group" class="input-group date form-group col-6" data-date-format="dd/mm/yyyy">
            <label for="ComplaintDate" class="col-form-label">Complaint Date:&nbsp;</label>
            @Html.EditorFor(model => model.ComplaintDate, "{0:mm/dd/yyyy}", new { htmlAttributes = new { @class = "form-control" } })
            <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
        </div>
    </div>

    // Model Fields deleted for brevity

    <div class="form-group row col-6">
            <div class="col-12 row">
                <div class="col-6">
                    <input type="submit" name="NewComplaint" class="btn btn-primary" value="Save" />
                </div>
                <div class="col-6">
                    <a href="@Url.Action("ComplaintList", new { wId = ViewBag.WreckerId })" class="btn btn-primary">Back to Complaints List</a>
                </div>
            </div>
        </div>
</div>

}

我的控制器:这是 posting 正确但模型为空

    [HttpPost]
    public ActionResult UpsertComplaint_Post(T_Complaints complaint)

    {

    // complaint is null here.
    // This is as far as I have gotten. I can't copmplete the post code until I
    // can get the model back to this point.

        return RedirectToAction("ComplaintList", new RouteValueDictionary(new { wId = complaint.WreckerID }));
    }

为了完整起见,这里是模型:

    public class T_Complaints
{
    public int Id { get; set; }
    [DataType(DataType.MultilineText)]
    public string Complaint { get; set; }
    [DataType(DataType.MultilineText)]
    [Display(Name = "Complainant Info")]
    public string ComplainantInfo { get; set; }
    [Required]
    public int WreckerID { get; set; }
    [Required]
    public bool Resolved { get; set; }
    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Complainant Date")]
    public DateTime ComplaintDate { get; set; }
    [DataType(DataType.Date)]
    [Display(Name = "Resolution Date")]
    public DateTime ResolutionDate { get; set; }
    [DataType(DataType.MultilineText)]
    public string Resolution { get; set; }
}

这是获取操作

       [HttpGet]
    public ActionResult UpsertComplaint(int? cid, string WreckerCo, int wId)
    {
        T_Complaints complaint = new T_Complaints();
        ViewBag.WreckerCo = WreckerCo;
        ViewBag.wId = wId;
        if (cid == null)

        // This is true so this is the block that executes on Add

        {
            complaint.ComplaintDate = DateTime.Now;
            complaint.WreckerID = wId;
            return View(complaint);
        }
        using (con)
        {
            con.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM [Towins].[dbo].[T_Complaints] WHERE Id = " + cid, con))
            {
                SqlDataReader reader = cmd.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        complaint.Resolved = (bool)reader["Resolved"];
                        complaint.Id = (int)reader["Id"];
                        complaint.Complaint = reader["Complaint"].ToString();
                        ViewBag.WreckerId = reader["WreckerID"];
                        complaint.ComplainantInfo = reader["ComplainantInfo"].ToString();
                        complaint.ComplaintDate = (DateTime)reader["ComplaintDate"];
                        if (reader["ResolutionDate"].ToString() != "")
                        {
                            complaint.ResolutionDate = (DateTime)reader["ResolutionDate"];
                        }
                        complaint.Resolution = reader["Resolution"].ToString();
                    };
                }
                reader.Close();
            }
            con.Close();
        }
        return View(complaint);
    }

好的。由于研究不充分并且没有 post 正确地回答问题,我得到了一些降级。我将重新尝试回答这个问题。我已经用测试代码简化了所有内容,并在此处重新post编辑它:

我的新测试模型完整:

namespace Towins.Models
{
    public class T_ComplaintsTest
    {
        public int Id { get; set; }
        public string Complaint { get; set; }
    }
}

我的新测试完整查看:

@model Towins.Models.T_ComplaintsTest

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

<h2>Add Complaint</h2>


@using (Html.BeginForm("AddComplaint", "Home", FormMethod.Post))
{
    <div class="text" style="text-align: center">
        <div class="form-row">
            <div class="form-group col-6">
                @Html.LabelFor(model => model.Complaint)
                @Html.EditorFor(model => model.Complaint, new { htmlAttributes = new { @class = "form-control" } })
            </div>
        </div>

        <div class="form-group row col-6">
            <div class="col-12 row">
                <div class="col-6">
                    <input type="submit" name="AddComplaint" class="btn btn-primary" value="Save" />
                </div>
            </div>
        </div>
    </div>
}

还有,我的完整操作方法:

    [HttpGet]
    public ActionResult AddComplaint()
    {
        T_ComplaintsTest complaint = new T_ComplaintsTest();
        complaint.Complaint = "Test";
        return View(complaint);
    }
    [HttpPost]
    public ActionResult AddComplaint(T_ComplaintsTest complaint)

    {
        //Breakpoint here.  Why is complaint null?
        return RedirectToAction("ComplaintList", new RouteValueDictionary(new { wId = 247 }));
    }

当 Get 发生时,它成功地在字段中呈现“Test”。当我点击提交按钮时,它停在断点“return RedirectToAction...” 为什么此时投诉为空?我看不出我的生命为什么它应该为空。视图属于此模型。它不应该在那里吗?

答案很简单,模型绑定器变得混乱了。

在您的 T_Complaints class 中,您有一个名为 Complaint 的 属性。
在您的 post 操作中,您使用投诉作为模型名称:

public ActionResult AddComplaint(T_ComplaintsTest complaint)

只需更改控制器中的模型名称将使模型绑定器的事情变得不那么复杂,并且您的模型不会为空。

public ActionResult AddComplaint(T_ComplaintsTest complaintsTest) // this won't be null anymore.