HTTPPost上的MVC5 ViewModel属性为null

时间:2016-03-24 18:42:50

标签: asp.net-mvc asp.net-mvc-5 http-post model-binding

我知道,此问题之前已被问过,但我还没有找到答案,我的例子似乎比大多数人简单得多。我确保模型数据在表单中,并确保Post操作与Get操作匹配;没有日期格式问题;我有类似的观点和控制器工作正常,我已经比较了。此示例使用传统路由,但我也尝试过属性路由,但行为没有变化。我甚至删除了EditorFor ReportName并将其放在HiddenFor中。隐藏的ReportID始终填充且正确,ReportName和ReportAbbr始终为null。 :(

首先,这是我的viewmodel:

namespace Core.AdminData
{
    [System.Runtime.Serialization.DataContract(Name = "MyReport")]
    public class MyReport
    {
        [System.Runtime.Serialization.DataMember(Name = "ReportID", Order = 0)]
        public int ReportID { get; set; }

        [System.Runtime.Serialization.DataMember(Name = "ReportName", Order = 1)]
        [Display(Name = "Name")]
        [Required(ErrorMessage = "Name Required")]
        public string ReportName { get; private set; }

        [System.Runtime.Serialization.DataMember(Name = "ReportAbbr", Order = 2)]
        [Display(Name = "Abbreviation")]
        [Required(ErrorMessage = "Abbreviation Required")]
        public string ReportAbbr { get; private set; }

        public MyReport()
        {
            ;
        }
    }
}

这是我的控制器:

public class ReportsController : Controller
{
    [HttpGet]
    public ActionResult TrendEdit(int id)
    {
        using (Client client = new Client(ConfigHelper.Protocol(), ConfigHelper.Host(), ConfigHelper.Port()))
        {
            MyReport rpt = null;
            try
            {
                rpt = client.GetReport(id);
                if (rpt == null)
                {
                    TempData["ErrorText"] = "Report does not exist";
                    return this.RedirectToAction("List");
                }
            }
            catch (Exception exc)
            {
                TempData["ErrorText"] = exc.Message;
                return this.RedirectToAction("List");
            }
            return View(rpt);
        }
    }

    [HttpPost]
    public ActionResult TrendEdit(MyReport report)
    {
        using (Client client = new Client(ConfigHelper.Protocol(), ConfigHelper.Host(), ConfigHelper.Port()))
        {
            try
            {
                if (!this.ModelState.IsValid || client.UpdateReport(report).StatusCode != SSStatus.Success)
                {
                    ViewBag.ErrorText = "An error occurred.  Please contact your web administrator for assistance.";
                    return View(report);
                }
            }
            catch (Exception exc)
            {
                ViewBag.ErrorText = exc.Message;
                return View(report);
            }
            return this.RedirectToAction("List");
        }
    }
}

最后,我的观点是:

@using Core.AdminData
@model MyReport

@{
    ViewBag.Title = "Edit Report";
    Layout = "~/Views/Shared/_ContentLayout.cshtml";
}

<div class="panel panel-default">
    <div class="panel-heading hidden-xs">
        <h4>Edit Report</h4>
    </div>

    @using (Html.BeginForm())
    {
        @Html.HiddenFor(model => model.ReportID)

        <div class="container-fluid panel-body">
            <div class="row">
                <div class="col-xs-12 message-error">@ViewBag.ErrorText</div>
            </div>
            <div class="row">
                <div class="col-xs-4 col-md-3 custom-label">
                    @Html.LabelFor(model => model.ReportName)
                </div>
                <div class="col-xs-8 col-md-9 custom-field">
                    @Html.EditorFor(model => model.ReportName)
                </div>
            </div>
            <div class="row">
                <div class="col-xs-offset-4 col-xs-8 col-md-offset-3 col-md-9 message-error">
                    @Html.ValidationMessageFor(model => model.ReportName)
                </div>
            </div>
            <div class="row">
                <div class="col-xs-4 col-md-3 custom-label">
                    @Html.LabelFor(model => model.ReportAbbr)
                </div>
                <div class="col-xs-8 col-md-9 custom-field">
                    @Html.EditorFor(model => model.ReportAbbr)
                </div>
            </div>
            <div class="row">
                <div class="col-xs-offset-4 col-xs-8 col-md-offset-3 col-md-9 message-error">
                    @Html.ValidationMessageFor(model => model.ReportAbbr)
                </div>
            </div>
            <div class="row">
                <div class="col-xs-4 col-md-3 text-right">
                    <input type="submit" value="Save" class="btn btn-default btn-sm" />
                </div>
            </div>
        </div>
    }
</div>

如果我错过了任何有用的代码,请告知我们。我希望这是显而易见的。我宁愿有一个&#34; Duh&#34;比不断与这段代码打架的时刻。 ;)TIA!

1 个答案:

答案 0 :(得分:2)

您的ReportNameReportAbbr媒体资源包含私有设置者,因此DefaultModelBinder无法设置该值。将它们更改为公共制定者

public string ReportName { get; set; }
public string ReportAbbr { get; set; }