我有一个控制器,视图和模型,用于我从其他人手中接管的应用程序。我遇到的问题是,当javascript提交表单时,我在Controller的Edit函数中的模型对象没有填充View中的值,所有字段都是null或0.我如何获取对象适当填补?
我试过看过Firebug,但我看不到任何表明问题的东西。字符串参数id正确地在URL中传递,例如:firsttry.com/admin/distribution/edit/sanfran
,其中sanfran是id字符串。 Firebug确认javascript在被调用时正确加载。
@section scripts
{
@Scripts.Render("~/bundles/qmmodules/admin/distribution")
}
DistributionController.cs
namespace JRI.QM.WebUI.Areas.Admin.Controllers
{
public class DistributionController : BaseAdminController
{
[HttpPost]
public ActionResult Edit(string id, DistributionViewModel model)
{
}
}
}
DistributionViewModel.cs
namespace JRI.QM.WebUI.Areas.Admin.Models.ViewModels
{
public class DistributionViewModel
{
public string name;
public string ipAddress;
public string description;
public Byte eyeDee;
public string destinationPath;
public List<JRI.DMP.BLL.Filter> filters;
}
}
Edit.cshtml
@model JRI.QM.WebUI.Areas.Admin.Models.ViewModels.DistributionViewModel
@{
ViewBag.Title = "Edit";
}
@using (Html.BeginForm("Edit", "Distribution", FormMethod.Post, new { id = "editRoleForm" }))
{
<div class="dialog" id="editRoleDialog">
@Html.TextBoxFor(x => x.name)
@Html.HiddenFor(x => x.ipAddress)
@Html.TextBoxFor(x => x.description)
@Html.HiddenFor(x => x.eyeDee)
@Html.HiddenFor(x => x.destinationPath)
@Html.HiddenFor(x => x.filters)
<div id="permissionsDialogFooter" class="dialogFooter newUser_dialogFooter">
<div class="qm_mediumButton submitButton">save</div>
<div class="qm_mediumButton cancelButton">cancel</div>
<br class="clearfloat" />
</div>
</div>
}
distribution.js
QM.ui.Role = (function ($) {
var _xhrInProgress = false;
function init() {
_bindEvents();
}
// private functions
function _bindEvents() {
var addEditRoleEvents = function (formObj) {
formObj.submit(function () {
var $this = $(this);
if ($this.valid()) {
$.post($this.attr("action"), $this.serialize(), function (response) {
if (response.Success) {
if (response.RedirectUrl) {
window.location.href = response.RedirectUrl;
}
}
});
}
return false;
});
formObj.find(".submitButton").click(function () {
formObj.submit();
});
};
// Edit role dialog
$(".dt-edit a").click(function () {
var $this = $(this);
$.get($this.attr("href"), function (data) {
$(".content").after(data);
QM.ui.Mask.show();
addEditRoleEvents($("#editRoleForm"));
});
return false;
});
}
return {
init: init,
};
})(jQuery);
$(document).ready(QM.ui.Role.init);
在RouteConfig.cs中
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "JRI.QM.WebUI.Controllers" }
);
答案 0 :(得分:1)
ASP.NET MVC的默认模型绑定器,仅适用于属性。您的模型类没有任何属性,它有字段。
在模型类中修复使用属性而不是字段,如下所示
public class DistributionViewModel
{
public string name { get; set; }
public string ipAddress { get; set; }
public string description { get; set; }
public Byte eyeDee { get; set; }
public string destinationPath { get; set; }
public List<JRI.DMP.BLL.Filter> filters { get; set; }
}
另外,我建议您按照C#代码转换,它说您应该使用 PascalCase 作为属性,您可以在此处查看更多详细信息https://msdn.microsoft.com/en-us/library/ms229043(v=vs.110).aspx
在对话之后,你的班级应该是那样的
public class DistributionViewModel
{
public string Name { get; set; }
public string IpAddress { get; set; }
public string Description { get; set; }
public Byte EyeDee { get; set; }
public string DestinationPath { get; set; }
public List<JRI.DMP.BLL.Filter> Filters { get; set; }
}