asp.net-mvc-4 – 发布后保留表单值(不是模型的一部分)
发布时间:2020-12-16 03:54:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个MVC4页面,其中包含一个表单,其中包含一组复选框,单选按钮和用作搜索字段的文本框.在发布后,将解析选择,并使用新结果更新下部结果网格.现在,所有表单值在返回时都被清除,新结果显示在网格中 – 只有网格是模型的一部分. 我希望所有表单选择在发布后保
我有一个MVC4页面,其中包含一个表单,其中包含一组复选框,单选按钮和用作搜索字段的文本框.在发布后,将解析选择,并使用新结果更新下部结果网格.现在,所有表单值在返回时都被清除,新结果显示在网格中 – 只有网格是模型的一部分.
我希望所有表单选择在发布后保留其值,以便用户可以查看(和更改)下一个帖子/搜索的选择.表格上印有viewbags. @using (Html.BeginForm("Index","Home",FormMethod.Post,new { id = "searchform" })) { @Html.ValidationSummary("Please correct the following errors") <div style="float:left;"> <div style="float:left;"> <label>Name:</label> @Html.TextBox("name") </div> <div style="float:left; margin-left:15px"> <label>Company:</label> @Html.TextBox("company") </div> <div style="float:left; margin-left:65px"> <label>Date Range:</label> @Html.TextBox("dateStart","",new { @class = "datefield",type = "date" }) to @Html.TextBox("dateEnd",type = "date" }) </div> </div> <div style="clear: both;"> Match Any Categories? <input type="radio" name="categoryMatchAll" value="false" checked="checked" /> Match All Categories? <input type="radio" name="categoryMatchAll" value="true" /> </div> <div style="float:left;"> <div id="searchform-categories" style="float:left;"> <div class="scroll_checkboxes"> <label>Categories</label> <ul> @foreach (var x in ViewBag.Categories) { <li> <input type="checkbox" name="categories" value="@x.Id"/> @x.Name </li> } </ul> </div> </div> <div id="searchform-diversity" style="float:left; margin-left:30px"> <div class="search-selection" style="float:left;"> <label>Minority Owned</label> <ul> @foreach (var x in ViewBag.Minorities) { <li> @Html.RadioButton("minorities",(String)x.Id.ToString()) @x.Name </li> } </ul> </div> <div class="search-selection" style="float:left;"> <label>Diversity Class</label> <ul> @foreach (var x in ViewBag.Classifications) { <li> @Html.RadioButton("classifications",(String)x.Id.ToString()) @x.Name </li> } </ul> </div> </div> </div> <div style="clear: both;"> <input type="submit" value="Search Profiles" /> <input type="submit" value="Reset" /> </div> } 数据网格绑定到模型中 @model IEnumerable<VendorProfileIntranet.Models.VendorProfile> <table id="VendorTable" width="100%" class="gradeA"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.CompanyName) </th> <th> @Html.DisplayNameFor(model => model.City) </th> <th> @Html.DisplayNameFor(model => model.State) </th> <th> @Html.DisplayNameFor(model => model.DateCreated) </th> <th>Actions</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td class="list-field"> @Html.DisplayFor(modelItem => item.Name) </td> <td class="list-field"> @Html.DisplayFor(modelItem => item.CompanyName) </td> <td class="list-field"> @Html.DisplayFor(modelItem => item.City) </td> <td> @Html.DisplayFor(modelItem => item.State) </td> <td class="list-field"> @Html.DisplayFor(modelItem => item.DateCreated) </td> <td class="list-field"> @Html.ActionLink("Edit","Edit",new { id = item.ProfileID }) | @Html.ActionLink("View","View",new { id = item.ProfileID }) | @Html.ActionLink("Delete","Delete",new { id = item.ProfileID },new { onclick = " return DeleteConfirm()" }) </td> </tr> } </tbody> <tfoot> <tr> <td> </td> </tr> </tfoot> 解决方法
所以这就是我通常如何解决这个问题.我的笔记纯粹是我的观点(宗教?)关于在MVC项目中命名类以明确其目的.
几个接口,以保持其可扩展性: // be specific about what type of results,both in the name of the // interface and the property needed,you don't want to have overlapping // properies on your classes,I like suffixing interfaces that are specific // to a View or Partial View with View public interface IPersonSearchResultsView { IEnumerable<EFPerson> PersonSearchResults { get; } } public interface IPersonSearchCriteriaView { PersonSearchCriteriaModel PersonSearchModel { get; } } 几个班级 // I like suffixing classes that I only use for MVC with Model public PersonSearchCriteriaModel { public string Name {get; set;} public string Company {get; set;} public string DateStart {get; set;} public string DateEnd {get; set;} } // I like suffixing classes that I used passed to a View/Partial View // with ViewModel public class PersonSearchViewModel : IPersonSearchResultsView,IPersonSearchCriteriaView { public IEnumerable<EFPerson> PersonSearchResults { get; set; } public PersonSearchCriteriaModel PersonSearchModel { get; set; } } 现在,对于您的控制器,我将以一种允许您将来执行Ajax的方式进行设置. public PersonController : Controller { public ActionResult Search() { var model = new PersonSearchViewModel(); // make sure we don't get a null reference exceptions model.PersonSearchModel = new PersonSearchCriteriaModel (); model.PersonSearchResults = new List<EFPerson>(); return this.View(model); } [HttpPost] public ActionResult Search(PersonSearchViewModel model) { model.PersonSearchResults = this.GetPersonResults(model.PersonSearchModel); return this.View(model) } // You could use this for Ajax public ActionResult Results(PersonSearchViewModel model) { model.PersonSearchResults = this.GetPersonResults(model.PersonSearchModel); return this.Partial("Partial-SearchResults",model) } private GetPersonResults(PersonSearchCriteriaModel criteria) { return DbContext.GetPersonResults(criteria) } } 创建几个部分视图您的视图. /Views/Person/Partial-SearchCriteria.cshtml @model IPersonSearchCriteriaView // the new part is for htmlAttributes,used by Ajax later @using (Html.BeginForm(...,new { id="searchCriteria" })) { // Here is were the magic is,if you use the @Html.*For(m=>) // Methods,they will create names that match the model // and you can back back to the same model on Get/Post <label>Name:</label> @Html.TextBoxFor(m => Model.PersonSearchModel.Name) // or let mvc create a working label automagically @Html.EditorFor(m => Model.PersonSearchModel.Name) // or let mvc create the entire form.. @Html.EditorFor(m => Model.PersonSearchModel) } /Views/Person/Partial-SearchResults.cshtml @model IPersonSearchResultsView @foreach (var person in Model.PersonSearchResults ) { <tr> <td class="list-field"> @Html.DisplayFor(modelItem => person.Name) </td> // etc </tr> } 最后看法: /Views/Person/Search.cshtml @model PersonSearchViewModel @Html.Partial("Partial-SearchCriteria",Model) // easily change the order of these <div id="searchResults"> @Html.Partial("Partial-SearchResults",Model); </div> 现在启用Ajax非常容易(简化了,我的不完全正确): $.Ajax({ url: '/Person/Results',data: $('#searchCriteria').serialize(),success: function(jsonResult) { $('#searchResults').innerHtml(jsonResult); }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在ASP.NET 5中添加一个TypeScript绝对类型的定义?
- asp.net-web-api-405使用AttributeRouting.PUTAttribute,除
- asp.net-mvc – 支持RavenDB的MVC4的会员系统
- asp.net core系列 43 Web应用 Session分布式存储(in memory
- asp现场抽奖,asp微信现场抽奖功能代码
- 是否可以在OSX上使用Razor作为独立库(没有任何asp.net业务)
- asp.net – 有人可以解释VistaDB吗?
- asp.net – 允许服务器/ usercontrol上的任何属性/属性
- asp.net-core – 如何在ConfigureServices中获取开发/分段/
- .Net Core技术研究-WebApi迁移ASP.NET Core2.0
推荐文章
站长推荐
- asp.net – 使PDF显示内联而不是单独的Acrobat R
- asp.net-mvc-3 – html.dropdownlist MVC3混乱
- 如何设置默认页面asp.net
- .net – 此程序集由比当前加载的运行时更新的运行
- asp.net webforms jquery =无法按下按钮
- iis – “立即启动应用程序池”和“启动模式Alwa
- ASP.NET – meta:ResourceKey vs <%$Resources
- ASP.NET MVC EF 连接数据库(二)-----Model Fir
- asp.net-mvc-5 – 在MVC 5期货中找不到Html.Seri
- asp.net-mvc – 如何为模型状态字典生成字段名称
热点阅读