asp.net-mvc – 模型绑定不起作用
发布时间:2020-12-16 04:22:45 所属栏目:asp.Net 来源:网络整理
导读:我有以下POCO课程: public class Location { public int LocationId { get; set; } public string Name { get; set; } public string Street { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { g
我有以下POCO课程:
public class Location { public int LocationId { get; set; } public string Name { get; set; } public string Street { get; set; } public string City { get; set; } public string State { get; set; } public string ZipCode { get; set; } public string Country { get; set; } public float? Latitude { get; set; } public float? Longitude { get; set; } public string PhoneNumber { get; set; } public string EmailAddress { get; set; } public string Website { get; set; } public virtual ICollection<Program> Programs { get; set; } public virtual ICollection<PlayerType> PlayerTypes { get; set; } } public class PlayerType { public int PlayerTypeId { get; set; } public string Name { get; set; } public int SortOrder { get; set; } public bool IsActive { get; set; } public virtual ICollection<Location> Locations { get; set; } } 和视图模型类 public class LocationViewModel { public Location Location { get; set; } public IList<PlayerType> SelectPlayerTypes { get; set; } public LocationViewModel() { Location = new Location(); } } 在我的创建表单中,我已将模型定义为 @model Locator.Models.LocationViewModel 并有以下字段: div class="editor-label"> @Html.LabelFor(model => model.Location.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Location.Name) @Html.ValidationMessageFor(model => model.Location.Name) </div> 在我的控制器中处理我的POST [HttpPost] public ActionResult Create(LocationViewModel location) { if (ModelState.IsValid) { locationRepository.InsertOrUpdate(location.Location); locationRepository.Save(); return RedirectToAction("Index"); } location.SelectPlayerTypes = golferTypeRepository.All.Where(p => p.IsActive).ToList(); return View(location); } 问题是我有一个Location对象,但没有任何属性设置为在表单中输入的值. 我在这里做错了吗? 谢谢 解决方法
这是问题所在:
[HttpPost] public ActionResult Create(LocationViewModel location) 你看到了吗?它是您的动作参数的名称:位置. 现在看一下你的视图模型,它有一个名为Location的属性: public Location Location { get; set; } 这会混淆模型绑定器.它不再知道您是否需要绑定LocationViewModel或其属性. 所以只需重命名以避免冲突: [HttpPost] public ActionResult Create(LocationViewModel model) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- .NET Core技术研究-中间件的由来和使用
- asp.net-mvc – 如何查看mvc应用程序中的所有视图
- asp.net – Internet Explorer的操作中止和延迟问题
- ASP.NET MVC MySql Membership Provider,用户无法登录
- asp.net – ELMAH日志如何按类型忽略错误
- 从asp.net mvc生成PDF文件
- asp.net – 什么是aspnet_Users和aspnet_Membership?我应该
- asp.net – 我是否需要viewstate来输入控件,如复选框,文本框
- asp.net-web-api – 如何使用ASP.NET标识设置Thinktecture
- asp.net-mvc – 如何将Home / Action / id映射到action / i
推荐文章
站长推荐
- ASP.NET与jQuery弹出对话框:如何回发对话框关闭
- .net – 如何保持验证DRY?
- Satellite Assembly,ASP.NET语言不变
- asp.net-mvc – 为什么visual studio 2012会在mv
- asp.net – 将url参数添加到asp主题文件夹中的cs
- asp.net-mvc-4 – Elmah.MVC 2.0.1 – 保护elmah
- asp.net – 实体框架:如何解决“FOREIGN KEY约束
- asp.net-core – ASP.NET Core 1.0 Synchronizat
- asp.net-mvc – 如何在backbone.js中保存模型集合
- 动手造轮子:实现一个简单的依赖注入(零)
热点阅读