Reputation: 195
I know, this question has been asked before, but I haven't found an answer yet and my example seems much simpler than most. I make sure the model data is inside the form and I made sure the Post action matches the Get action; there are no date formatting issues; and I have similar views & controllers that work fine which I've compared to. This example uses conventional routing, but I've tried attribute routing as well, with no change in behavior. I have even removed the EditorFor ReportName and put it in a HiddenFor instead. The Hidden ReportID is always populated and correct, the ReportName and ReportAbbr are always null. :(
First, here is my 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()
{
;
}
}
}
Here is my controller:
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");
}
}
}
And finally, here is my view:
@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>
If I've missed any code that would be helpful, please let me know. I hope this is something obvious. I'd rather have a "Duh" moment than to keep fighting with this code. ;) TIA!
Upvotes: 1
Views: 530
Reputation:
Your ReportName
and ReportAbbr
properties have private setters, so the DefaultModelBinder
cannot set the value. Change them to have public setters
public string ReportName { get; set; }
public string ReportAbbr { get; set; }
Upvotes: 2