asp.net-mvc – 为什么我的ViewModel在[HttpPost]上为空? .NET
发布时间:2020-12-16 03:26:37 所属栏目:asp.Net 来源:网络整理
导读:我正努力在我的Web应用程序中正确使用ViewModels,但我遇到了各种各样的问题.其中之一是,如果我在使用Create操作发布后立即设置断点,则我的viewModel没有存储任何表单值.我一定做错了什么,但我尝试了一些事情.包括下面的代码,我将表单项命名为与viewModel字段
我正努力在我的Web应用程序中正确使用ViewModels,但我遇到了各种各样的问题.其中之一是,如果我在使用Create操作发布后立即设置断点,则我的viewModel没有存储任何表单值.我一定做错了什么,但我尝试了一些事情.包括下面的代码,我将表单项命名为与viewModel字段相同,以查看是否有帮助.
我也想知道你的viewmodel中应该代表什么属性.我见过人们在博文中使用不同的东西和诸如此类的东西. 如果视图将呈现一个选择列表,那么我认为viewmodel应该为此保存一个IEnumerable SelectListItem,如下所示.然而,我看到人们使用IEnumerable Entity来表示选择列表所代表的类型. 任何人都可以为我阐明这一点吗?我昨晚取消了整个业务逻辑,所以我可以重新开始尝试并正确地完成它. 我的ViewModel: public class ServerCreateViewModel { public int Id { get; set; } // CompanyName represents a field in the Company model. I did this to see if // it would help with model binding. Beforehand it was Companies to represent the type. I've done the same for the rest of them,so I wont comment on this again. public IEnumerable<SelectListItem> CompanyName { get; set; } // Represents the Game model. public IEnumerable<SelectListItem> GameTitle { get; set; } //Represents the Location model,etc... public IEnumerable<SelectListItem> City { get; set; } public IEnumerable<SelectListItem> NumberOfPlayers { get; set; } public IEnumerable<SelectListItem> CurrencyAbbreviation { get; set; } } 我的控制器动作: public ActionResult Create() { var viewModel = new ServerCreateViewModel(); viewModel.CompanyName = new SelectList(_dataService.Companies.All(),"Id","CompanyName"); viewModel.GameTitle = new SelectList(_dataService.Games.All(),"GameTitle"); viewModel.City = new SelectList(_dataService.Locations.All(),"City"); viewModel.NumberOfPlayers = new SelectList(_dataService.ServerPlayers.All(),"NumberOfPlayers"); return View(viewModel); } [HttpPost] public ActionResult Create(FormCollection collection,ServerCreateViewModel viewModel) { try { // I put a breakpoint in here to check the viewModel values. // If I dont pass the viewModel into the constructor,it doesnt exist. // When I do pass it in,its empty. return Content("Success"); } catch { return Content("Fail"); } } 我的看法: @model GameserverCompare.ViewModels.Server.ServerCreateViewModel @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Server</legend> @Html.HiddenFor(m => m.Id) <div class="editor-label"> @Html.LabelFor(model => model.CompanyName) </div> <div class="editor-field"> @Html.DropDownListFor(m => Model.CompanyName,Model.CompanyName) @Html.ValidationMessageFor(model => model.CompanyName) </div> <div class="editor-label"> @Html.LabelFor(model => model.GameTitle) </div> <div class="editor-field"> @Html.DropDownListFor(m => Model.GameTitle,Model.GameTitle) @Html.ValidationMessageFor(model => model.GameTitle) </div> <div class="editor-label"> @Html.LabelFor(model => model.City) </div> <div class="editor-field"> @Html.DropDownListFor(m => Model.City,Model.City) @Html.ValidationMessageFor(model => model.City) </div> <div class="editor-label"> @Html.LabelFor(model => model.NumberOfPlayers) </div> <div class="editor-field"> @Html.DropDownListFor(m => Model.NumberOfPlayers,Model.NumberOfPlayers) @Html.ValidationMessageFor(model => model.NumberOfPlayers) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } 解决方法
由于您在表单模型中使用SelectList属性,因此您需要使用不同的模型来表示这些列表中的选定值:
public class ServerCreatePostbackModel { public int Id { get; set; } // CompanyName represents a field in the Company model. public string CompanyName { get; set; } // Represents the Game model. public string GameTitle { get; set; } //Represents the Location model,etc... public string City { get; set; } public int NumberOfPlayers { get; set; } public string CurrencyAbbreviation { get; set; } } 让你的HttpPost动作将其中一个作为其参数. 哦,并确保使用HiddenFor作为Id属性,因此它将与其他数据一起发回. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在Asp.net MVC中导出到Excel [已关闭]
- asp.net-mvc – 在每个网址的末尾添加尾部斜杠?
- asp.net – 在Repeater控件中实现功能链接
- asp.net-mvc – MVC的HTTP缓存 – Last-Modified响应头始终
- asp.net-mvc – 无法加载汇编WebPages.Deployment版本2在主
- 如何使用IIS 6在asp.net中实现远期未来的过期标头
- ASP.NET -- WebForm -- ScriptManager 类
- asp.net – 如何绑定DropDownList的选定值
- ASP.Net OData在LB中通过SSL终止失败
- asp.net-mvc – Css和脚本不工作,直到用户登录网站 – Asp.