【问题标题】:Passing a selected value from a partial view to the main view's viewmodel将部分视图中的选定值传递给主视图的视图模型
【发布时间】:2011-10-14 13:46:02
【问题描述】:

作为 ASP.Net MVC3 新手,我们有一个需要帮助的问题。 (这个帖子底部也有问题。)

首先,我不确定以下是否是解决此问题的最佳方法,所以如果我们走错了方向,请告诉我。我们想使用部分视图来查找下拉列表。在某些情况下,查找将在多个地方完成,而且数据不是我们视图模型的一部分。数据可能来自我们应用程序中的数据库或 Web 服务。有些数据是在启动时加载的,有些是基于表单中选择的其他值。

我们从主视图调用子操作,并返回带有我们获得的数据的局部视图。一旦用户选择了他们的选择,我们就不确定如何将所选项目代码存储在我们的主视图模型中。

在我们的主窗体中,我们调用一个动作:

@model Apps.Model.ViewModels.AVMApplicationInfo
        ...
        <div class="editor-label">
            @Html.LabelFor(m => m.VMResidencyWTCS.DisplayState) 
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(m => m.VMResidencyWTCS.DisplayState)
            @Html.DropDownListFor(m => m.VMResidencyWTCS.DisplayState, Apps.Model.Helpers.ResidencyStates.StateList)
            @Html.ValidationMessageFor(m => m.VMResidencyWTCS.DisplayState)
        </div>
        @Html.Action("DisplayCounties", "PersonalInfo")
        ...

在 PersonalInfo 控制器中:

    [ChildActionOnly]
    public ActionResult DisplayCounties()
    {
        IList<County> countiesDB = _db.Counties
            .OrderBy(r => r.CountyDescr)
            .Where(r => r.State == "WI"
             && r.Country == "USA")
            .ToList();

        //Create an instance of the county partial view model
        VMCounty countyView = new VMCounty();

        //Assign the available counties to the view model
        countyView.AvailableCounties = new SelectList(countiesDB, "CountyCd", "CountyDescr");

        return PartialView("_DisplayCounties", countyView);
    }

在 _DisplayCounties 局部视图中:

@model Apps.Model.ViewModels.VMCounty 
<div class="editor-label"> 
    @Html.LabelFor(m => m.CountyDescr) 
</div> 
<div class="editor-field"> 
    @Html.DropDownListFor(x => x.SelectedCountyCd, Model.AvailableCounties) 
</div>

如何将 SelectedCountyCd 分配给主表单视图模型 (Apps.Model.ViewModels.AVMApplicationInfo ) 中的字段?何时调用子动作/部分视图是否存在任何问题;即,它是否在启动时加载,是否可以使用此方法将用户选择作为查找过滤器包含在内?如果是这样,如何将值传递给子控制器;查看包?

【问题讨论】:

    标签: asp.net-mvc-3 html.dropdownlistfor asp.net-mvc-partialview


    【解决方案1】:

    您可以将其作为参数传递给子操作:

    @model Apps.Model.ViewModels.AVMApplicationInfo
    ...
    @Html.Action("DisplayCounties", "PersonalInfo", new { 
        selectedCountyCd = Model.CountyCd // or whatever the property is called
    })
    

    然后让子动作将此值作为参数:

    [ChildActionOnly]
    public ActionResult DisplayCounties(string selectedCountyCd)
    {
        IList<County> countiesDB = _db.Counties
            .OrderBy(r => r.CountyDescr)
            .Where(r => r.State == "WI"
             && r.Country == "USA")
            .ToList();
    
        //Create an instance of the county partial view model
        VMCounty countyView = new VMCounty();
    
        //Assign the available counties to the view model
        countyView.AvailableCounties = new SelectList(countiesDB, "CountyCd", "CountyDescr");
    
        // assign the selected value to the one passed as parameter from the main view
        countyView.SelectedCountyCd = selectedCountyCd;
    
        return PartialView("_DisplayCounties", countyView);
    }
    

    【讨论】:

    • 我认为您的建议将允许我将用户选择作为查找过滤器传递。也许更像这样:@model Apps.Model.ViewModels.AVMApplicationInfo ... @Html.Action("DisplayCounties", "PersonalInfo", new { state = Model.State // or whatever the property is called }) 然后控制器会喜欢以下内容:[ChildActionOnly] public ActionResult DisplayCounties(string state) { IList&lt;County&gt; countiesDB = _db.Counties .OrderBy(r =&gt; r.CountyDescr) .Where(r =&gt; r.State == **state** &amp;&amp; r.Country == "USA") .ToList();
    • 所以调用部分操作的结果是在我的主视图中显示一个带有县下拉列表的部分视图。 (目前有效。)知道如何将选定的代码分配回主视图模型吗?
    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 2023-04-01
    相关资源
    最近更新 更多