我有一个问题,当我的剃刀视图没有提供更新的模型对象到控制器时,表单发布到控制器进行更新。剃刀视图不会发布更新后的模型数据到控制器 - 数据绑定失败
该视图旨在允许用户对现有记录进行更改并将其保存回数据库。多个可更新记录使用'@foreach'显示在表单上。
控制器向视图(GET)提供了正确的数据,但无论我尝试什么,每次POST返回或“保存”数据时,返回给控制器的对象都是原始对象。当用户返回控制器保存更新时,用户在视图中所做的任何更改都不会反映到对象中。
P.S.不要被我的视图中的“控制器”一词所迷惑 - 这是实体名称正在更新,而不是MVC控制器。
这里是控制器代码:
public partial class DeviceStationController : Controller
{
// GET:
public ActionResult MyDevicesSetup()
{
var tblUserDevice = db.TblUserDevices.Include(x => x.TblDevice).Include(x => x.TblDevicePrograms).Include(x => x.TblDeviceSensors).Include(x => x.TblDeviceStations).Include(x => x.TblUser).Where(x => x.TblUser.AspNetUser.UserName == User.Identity.Name);
var UserDevice_IDs = tblUserDevice.Select(p => p.Device_ID).Distinct();
var tblDevice = db.TblDevices.Include(x => x.TblUserDevices).Where(x => UserDevice_IDs.Contains(x.Device_ID));
return View(tblDevice.ToList());
}
// POST:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult MyDevicesSetup([Bind(Include = "Device_ID,DeviceCode,DeviceName,TimeZone,ExtBoards,Sequential,StationDelay,MasterStation,MastOnOffset,MastOffOffset,LocationZip,LocationCity,LocationCountry,DownloadFlag,LastDownload,LastUpload,RecordCreated,RecordEdited,RecordDeleted")] TblDevice tblDevice)
{
if (ModelState.IsValid)
{
tblDevice.RecordEdited = DateTime.Now;
db.Entry(tblDevice).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("MyDevicesSetup");
}
return View(tblDevice);
}
}
这里是查看代码:
@model IEnumerable<AquaTame.Models.TblDevice>
@{
ViewBag.Title = "Controller Setup";
}
@foreach (var controller in Model)
{
<div data-role="collapsible" data-collapsed-icon="carat-d" data-expanded-icon="carat-u" data-iconpos="right">
<h4>
@controller.DeviceName
</h4>
@using (Html.BeginForm("MyDevicesSetup", "DeviceStation", routeValues: controller))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => controller.Device_ID)
<div class="form-group">
@Html.LabelFor(model => controller.DeviceCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => controller.DeviceCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => controller.DeviceCode, "", new { @class = "text-danger" })
</div>
</div>
@*More fields here..... removed for brevity...*@
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
呈现给浏览器的HTML表单标签是在这里:
<form action="/DeviceStation/MyDevicesSetup?TblUserDevices=System.Collections.Generic.HashSet%601%5BAquaTame.Models.TblUserDevice%5D&Device_ID=1&DeviceCode=MX24B&DeviceName=%231-w&TimeZone=-8&ExtBoards=0&Sequential=True&StationDelay=5&MasterStation=False&MastOnOffset=0&MastOffOffset=0&LocationZip=97124&DownloadFlag=False&RecordCreated=05%2F04%2F2014%2000%3A00%3A00&RecordEdited=07%2F14%2F2014%2015%3A58%3A15&RecordDeleted=True" method="post">
//lots of stuff here ....
</form>
的网址get的POST是如预期的那样;包含源自GET的原始元素:
http://localhost:59259/DeviceStation/MyDevicesSetup?TblUserDevices=System.Collections.Generic.HashSet%601%5BAquaTame.Models.TblUserDevice%5D&Device_ID=1&DeviceCode=MX24B&DeviceName=%231-w&TimeZone=-8&ExtBoards=0&Sequential=True&StationDelay=5&MasterStation=False&MastOnOffset=0&MastOffOffset=0&LocationZip=97124&DownloadFlag=False&RecordCreated=05%2F04%2F2014%2000%3A00%3A00&RecordEdited=07%2F14%2F2014%2016%3A10%3A36&RecordDeleted=True
下面是更多从窗体中呈现的HTML。这些属性名称似乎保留得很好,但这超出了我理解“底层”情况的能力。我感谢所有有用的意见和建议:
<form action="/DeviceStation/MyDevicesSetup?TblUserDevices=System.Collections.Generic.HashSet%601%5BAquaTame.Models.TblUserDevice%5D&Device_ID=1&DeviceCode=MX24B&DeviceName=%231-w&TimeZone=-8&ExtBoards=0&Sequential=True&StationDelay=5&MasterStation=False&MastOnOffset=0&MastOffOffset=0&LocationZip=97124&DownloadFlag=False&RecordCreated=05%2F04%2F2014%2000%3A00%3A00&RecordEdited=07%2F14%2F2014%2016%3A11%3A21&RecordDeleted=True" method="post">
<input name="__RequestVerificationToken" type="hidden" value="7-wHfkHpP50iZ4pekCWhIe0ahkvvE7KapaJjFkEhBfjZwtu8-bBfJvG1Pg-9ILn0FsXnrj8Jq1TJQKrq5DxQkaLxd7AVcxsQjqJegrwJL4VDIeR5H68QEPmqOQOu9AIIfdYzqON-iUDv4dFGg5IkXg2">
<div class="form-horizontal">
<input data-val="true" data-val-number="The field Device_ID must be a number." data-val-required="The Device_ID field is required." id="controller_Device_ID" name="controller.Device_ID" type="hidden" value="1">
<div class="form-group">
<label class="control-label col-md-2" for="controller_DeviceCode">DeviceCode</label>
<div class="col-md-10">
<div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
<input class="form-control text-box single-line" id="controller_DeviceCode" name="controller.DeviceCode" type="text" value="MX24B"></div>
<span class="field-validation-valid text-danger" data-valmsg-for="controller.DeviceCode" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="controller_DeviceName">DeviceName</label>
<div class="col-md-10">
<div class="ui-input-text ui-body-inherit ui-corner-all ui-shadow-inset">
<input class="form-control text-box single-line" data-val="true" data-val-length="The field DeviceName must be a string with a maximum length of 8." data-val-length-max="8" id="controller_DeviceName" name="controller.DeviceName" type="text" value="#1-w"></div>
<span class="field-validation-valid text-danger" data-valmsg-for="controller.DeviceName" data-valmsg-replace="true"></span>
</div>
</div>
@*More form fields in here, removed for brevity........*@
<div class="ui-btn ui-input-btn ui-corner-all ui-shadow">Save<input type="submit" value="Save" class="btn btn-default"></div>
</div>
</form>
我正在取得一些进展,每个人的帮助。我是MS MVC实体框架的新成员,并且自学成才,所以我恐怕缺乏一些关键知识。我已经从HTML BeginForm帮助器中删除了“controller”对象参数,并确保表单具有所有可用的字段。我已经将绑定前缀“控制器”添加到控制器上的绑定(非常令人困惑,我知道)。我现在收到返回给控制器的空数据,所以我仍然要做一些错误的事情。
这里是控制器代码:
[HttpPost]
[ValidateAntiForgeryToken]
//public ActionResult MyDevicesSetup([Bind(Prefix = "controller")] TblDevice tblDevice)
public ActionResult MyDevicesSetup([Bind(Prefix = "controller", Include = "Device_ID,DeviceCode,DeviceName,TimeZone,ExtBoards,Sequential,StationDelay,MasterStation,MastOnOffset,MastOffOffset,LocationZip,LocationCity,LocationCountry,DownloadFlag,LastDownload,LastUpload,RecordCreated,RecordEdited,RecordDeleted")] TblDevice tblDevice)
{
if (ModelState.IsValid)
{
tblDevice.RecordEdited = DateTime.Now;
db.Entry(tblDevice).State = EntityState.Modified;
db.SaveChanges();
//return RedirectToAction("MyDevicesSetup");
}
return View(tblDevice);
}
}
这里是Razor视图代码:
@model IEnumerable<AquaTame.Models.TblDevice>
@{
ViewBag.Title = "Controller Setup";
}
@foreach (var controller in Model)
{
<div data-role="collapsible" data-collapsed-icon="carat-d" data-expanded-icon="carat-u" data-iconpos="right">
<h4>
@controller.DeviceName
</h4>
@*@using (Html.BeginForm("MyDevicesSetup", "DeviceStation", routeValues: controller))*@
@using (Html.BeginForm("MyDevicesSetup", "DeviceStation"))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => controller.Device_ID)
@Html.HiddenFor(model => controller.DownloadFlag)
@Html.HiddenFor(model => controller.LastDownload)
@Html.HiddenFor(model => controller.LastUpload)
@Html.HiddenFor(model => controller.RecordCreated)
@Html.HiddenFor(model => controller.RecordEdited)
@Html.HiddenFor(model => controller.RecordDeleted)
<div class="form-group">
@Html.LabelFor(model => controller.DeviceCode, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => controller.DeviceCode, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => controller.DeviceCode, "", new { @class = "text-danger" })
</div>
</div>
@*Remaining form fields follow - left out for brevity...*@
<input type="submit" value="Save" class="btn btn-default" />
</div>
}
最后,在这里被发送到控制器从POST数据:
__RequestVerificationToken:NO1PIEHxKyzlNldFuOOt-
EZhiU5VDe1Ax8CI9xzEzvViSgBqECKzmLrDRIdJeNOzMMJDaA-GTTBt5OeueTvnCKK6hK7MK_51EtANrVjwb5m4zVQY6tlHbnJcw3mdO9m5YhZi2eskyq4NhoHJHooL0g2
controller.Device_ID:1
controller.DownloadFlag:False
controller.LastDownload:
controller.LastUpload:
controller.RecordCreated:5/4/2014 12:00:00 AM
controller.RecordEdited:7/14/2014 4:11:21 PM
controller.RecordDeleted:True
controller.DeviceCode:MX24c
controller.DeviceName:#1-w
controller.TimeZone:-8
controller.ExtBoards:0
controller.Sequential:true
controller.Sequential:false
controller.StationDelay:5
controller.MasterStation:false
controller.MastOnOffset:0
controller.MastOffOffset:0
controller.LocationZip:97124
controller.LocationCity:
controller.LocationCountry:
当你拿起你的形式之一,并提交,什么是实际的HTML标记那种形式? POST请求中捕获的值是什么? (您可以使用浏览器调试工具(如Firebug或Chrome工具)检查这些工具。) – David
用for循环替换每个 –
在POST上,数据库中的数据是否正在更新? – Dbloch