简体   繁体   English

在不使用 AJAX 的情况下重新加载 ASP.NET MVC3 部分视图

[英]Reloading an ASP.NET MVC3 Partial View without using AJAX

I have an MVC3 application with Razor and I created a View that inside renders a Partial View.我有一个带有 Razor 的 MVC3 应用程序,并且我创建了一个内部呈现部分视图的视图。 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:_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" />

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.我希望在提交两个DropDownLists时保持它们的状态,即用户选择的值,当视图重新加载过滤数据时。

Is there any way to do it without using AJAX?不使用 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弗朗切斯科

When you submit the form to the corresponding controller action, this action should take as input parameter some view model.当您将表单提交给相应的 controller 动作时,该动作应作为输入参数 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.然后 controller 操作可以返回相同的视图,该视图将保留下拉框的选定值。

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:我建议您使用编辑器模板而不是渲染部分,因为这将确保下拉列表的正确命名并最终保留选定的值:

@Html.EditorFor(x => x.SearchFilters)

Without ajax not, or you will have to repost the whole form.没有 ajax 没有,否则您将不得不重新发布整个表格。 MVC is a web framework which is not dynamic like a winforms application. MVC 是一个 web 框架,它不像 winforms 应用程序那样是动态的。 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.您必须将更改发布到 controller 并重新加载包含必要更改的页面,或使用 ajax 重新加载这些更改。

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.您可以提供YearMonth属性的默认值(在第一次请求时选择)并绑定它们而不是您提供的硬编码启动值。

So instead of:所以而不是:

@Html.DropDownListFor(m => m.Year, new SelectList(Model.YearsList, "Value", "Text"), DateTime.Now.Year)

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.顺便说一句,这似乎是错误的,因为应将所选值(我认为DateTime.Now.Year在您的示例中)作为SelectList的构造函数(而不是DropDownListFor方法的)参数提供。 DropDownListFor method doesn't have a 'selected value' argument. DropDownListFor 方法没有“选定值”参数。

You could write:你可以写:

@Html.DropDownListFor(m => m.Year, new SelectList(Model.YearsList, "Value", "Text", Model.Year))

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).这将使下拉菜单在使用发布的 model 呈现时保留所选值(如 Model.Year 和 Model.Month 将保留这些值)。 So you should make sure those values won't get overwritten with default ones after subsequent submits.因此,您应该确保在后续提交后这些值不会被默认值覆盖。

I don't have IDE at this time so couldn't test but this might work:我目前没有 IDE 所以无法测试,但这可能有效:

Choose Month选择月份

EDIT:编辑:

 @Html.DropDownListFor(m => m.Month, 
Model.MonthsList.Select(
        t => new SelectListItem {
                 Text = t.Name,
                 Value = t.Value,
                 Selected = t.Value == Model.Month,
        },

 Model.Month.ToString(), new { @disabled = "disabled" })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM