将选定值从局部视图传递到主视图的视图模型

发布于 2024-12-09 20:39:26 字数 2077 浏览 1 评论 0原文

作为 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 ) 中的字段?何时调用子操作/部分视图是否存在任何问题?即,它是否在启动时加载,并且此方法可以用于包含用户选择作为查找过滤器吗?如果是这样,如何将值传递给子控制器?查看包?

As ASP.Net MVC3 newbies we have an issue that would like assistance with. (Questions at the bottom of this thread too.)

First of all I am not sure if the following is the best way to go about this so please let me know if we are heading in the wrong direction. We would like to use partial views to do lookups for dropdown lists. In some cases the lookups will be done in multiple places and also, the data is not part of our viewmodel. The data may be coming from a Database or Web Service in our application. Some of the data is loaded at startup and some is based upon other values selected in the form.

We are calling a child action from our main view and returning a partial view with the data we obtained. Once the user selects their choice we are not sure how to store the selected item code in our main view model.

In our main form we call to an action:

@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")
        ...

In the PersonalInfo controller:

    [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);
    }

In the _DisplayCounties partial view:

@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>

How do I assign the SelectedCountyCd to a field in the main form view model (Apps.Model.ViewModels.AVMApplicationInfo )? Are there any issues of when the child action/partial view is called; i.e., is it loaded at start up and can this method be used to include a user choice as a filter for the lookup? If so, how could the value be passed to the child controller; viewbag?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

第几種人 2024-12-16 20:39:26

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

@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);
}

You could pass it as parameter to the child action:

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

and then have the child action take this value as parameter:

[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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文