不使用 AJAX 重新加载 ASP.NET MVC3 部分视图
我有一个带有 Razor 的 MVC3 应用程序,我创建了一个在内部呈现部分视图的视图。主视图如下所示:
@{Html.RenderPartial("_SearchFilters", Model.SearchFilters);}
@* Other HTML elements *@
在 _SearchFilters 部分视图 内,我在 Form 元素内有以下 DropDownLists:
Choose Year
@Html.DropDownListFor(m => m.Year, new SelectList(Model.YearsList, "Value", "Text"), DateTime.Now.Year)
Choose Month
@Html.DropDownListFor(m => m.Month, new SelectList(Model.MonthsList, "Value", "Text"), Model.Month.ToString(), new { @disabled = "disabled" })
<input type="submit" value="Display" />
我希望在 Submit 上有两个 DropDownLists当使用过滤后的数据重新加载视图时, 保留其状态,即用户选择的值。
有没有什么方法可以不使用AJAX来做到这一点?
更新
ViewModel 如下:
public class TableSearchFiltersViewModel
{
public bool YTM { get; set; }
public int? Month { get; set; }
public int? Year { get; set; }
public IEnumerable<SelectListItem> YearsList
{
get
{
return Enumerable.Range(2011, (DateTime.Now.Year - 2011 + 4)).Select(m => new SelectListItem
{
Value = m.ToString(),
Text = m.ToString(),
}).OrderBy(m => m.Value);
}
}
public IEnumerable<SelectListItem> MonthsList
{
get
{
return Enumerable.Empty<SelectListItem>();
}
}
}
谢谢
Francesco
I have an MVC3 application with Razor and I created a View that inside renders a Partial View. This is how the main View looks like:
@{Html.RenderPartial("_SearchFilters", Model.SearchFilters);}
@* Other HTML elements *@
Inside the _SearchFilters Partial View I have the following DropDownLists inside a Form element:
Choose Year
@Html.DropDownListFor(m => m.Year, new SelectList(Model.YearsList, "Value", "Text"), DateTime.Now.Year)
Choose Month
@Html.DropDownListFor(m => m.Month, new SelectList(Model.MonthsList, "Value", "Text"), Model.Month.ToString(), new { @disabled = "disabled" })
<input type="submit" value="Display" />
I would like that upon Submit the two DropDownLists keep their status, namely the value selected by the user, when the View is reloaded with the filtered data.
Is there any way to do it without using AJAX?
UPDATE
The ViewModel is as follows:
public class TableSearchFiltersViewModel
{
public bool YTM { get; set; }
public int? Month { get; set; }
public int? Year { get; set; }
public IEnumerable<SelectListItem> YearsList
{
get
{
return Enumerable.Range(2011, (DateTime.Now.Year - 2011 + 4)).Select(m => new SelectListItem
{
Value = m.ToString(),
Text = m.ToString(),
}).OrderBy(m => m.Value);
}
}
public IEnumerable<SelectListItem> MonthsList
{
get
{
return Enumerable.Empty<SelectListItem>();
}
}
}
Thanks
Francesco
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您将表单提交给相应的控制器操作时,该操作应将某些视图模型作为输入参数。该视图模型的属性将从表单中包含的输入字段(包括两个下拉列表的选定值)绑定。然后控制器操作可以返回相同的视图,该视图将保留下拉框的选定值。
我建议您使用编辑器模板而不是渲染部分内容,因为这将确保下拉列表的正确命名并最终保留选定的值:
When you submit the form to the corresponding controller action, this action should take as input parameter some view model. This view model's properties will be bound from the input fields contained in the form including the selected value of the two dropdowns. Then the controller action could return the same view which will preserve the selected values of the dropdown boxes.
I would recommend you to use Editor Templates though instead of rendering partials as this will ensure proper naming of the dropdowns and eventually preserve selected values:
我目前没有 IDE,所以无法测试,但这可能有效:
选择月份
编辑:
I don't have IDE at this time so couldn't test but this might work:
Choose Month
EDIT:
如果没有ajax则不行,否则你将不得不重新发布整个表单。 MVC 是一个 Web 框架,它不像 winforms 应用程序那样是动态的。您必须将更改发布到控制器并重新加载包含必要更改的页面,或者使用 ajax 重新加载这些更改。
Without ajax not, or you will have to repost the whole form. MVC is a web framework which is not dynamic like a winforms application. You will have to post the changes to your controller and reload the page with the necessary changes, or use ajax to reload these changes.
您可以为 Year 和 Month 属性提供默认值(在第一次请求时选择)并绑定这些值,而不是您提供的硬编码启动值。
因此,而不是:
顺便说一句似乎是错误的,因为所选值(我认为 DateTime.Now.Year 在您的示例中)应该作为 SelectList 提供构造函数(而不是 DropDownListFor 方法的)参数。 DropDownListFor 方法没有“选定值”参数。
编写:
您可以在第二个下拉列表中
和类似的内容。 这将使下拉列表在使用发布的模型渲染时保留所选值(因为 Model.Year 和 Model.Month 将保留这些值)。因此,您应该确保在后续提交后这些值不会被默认值覆盖。
You could provide the default values for Year and Month properties (to be selected at the first request) and bind those instead of the hardcoded startup values you provided.
So instead of:
Which btw seems erroneous, as selected value (which I suppose DateTime.Now.Year is in your example) should be provided as SelectList's constructor (instead of DropDownListFor method's) argument. DropDownListFor method doesn't have a 'selected value' argument.
You could write:
and analogously in the second dropdown.
This will make dropdowns keeps the selected values when rendered using the posted model (as Model.Year and Model.Month would hold those). So you should make sure those values won't get overwritten with default ones after subsequent submits.