Razor MVVM中的局部视图

问题描述 投票:0回答:1

这是.net核心MVVM应用程序。我正在尝试实现局部视图以呈现从数据库填充的下拉列表。最初,我是通过视图组件完成此操作的,然后才发现您显然无法对视图组件的各种DOM元素进行操作(特别是无法通过javascript重新选择选项),因此我尝试了部分视图。我无法将逻辑连接到局部视图的后面。

这是我的代码:_Partial.cshtml


@model XXX.Source


    @Html.DropDownList(Model.FieldName,
    new List<SelectListItem>(Model.OptionList),
    "Choose",
    new
    {
        @multiple = "multiple",
        @class = "input-sm",
        @size = 4
    });

模型来源:

 public class Source
        {
            public IEnumerable<SelectListItem> OptionList { get; set; }
            public string FieldName { get; set; }
        }

我在这里叫它

@Html.RenderPartialAsync("_Partial", new ViewDataDictionary(ViewData) { { "FieldName", "Source" } });

这是部分视图背后的代码:


namespace XXX
{
    public class PartialModel : PageModel
    {
        public IService _Service;

        public PartialModel(IService Service)
        {
            _Service = Service;
        }



        public async Task<ActionResult> OnGet(string fieldName)
        {


            SourceOption viewModel = new SourceOption();

            var items = await _Service.GetSourceAsync();
            viewModel.FieldName = fieldName;
            viewModel.OptionList =
                from c in items
                select new SelectListItem
                {
                    Value = c.Sorid.ToString(),
                    Text = c.Name
                };


            return new PartialViewResult()
            {
                ViewName = "Partial",
                ViewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = viewModel
                }
            };
        }
    }
}

它会生成并运行,但在生成的HTML中只剩下垃圾-“ System.Threading.Tasks.Task`1 [System.Threading.Tasks.VoidTaskResult];”。我试图在我的OnGet方法上设置一个断点,但是它从未到达断点,因此我显然缺少了一些东西。有人可以提供一个执行此操作的示例(不是MVC)来查看我出了问题的地方吗?

.net asp.net-core mvvm partial-views
1个回答
0
投票

如果要使用ViewData将数据传递到页面,然后渲染局部视图并指定要传递到该局部视图的模型,则可以参考以下示例。

在.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

@{
    await Html.RenderPartialAsync("_Partial", ViewData["Source"]);
}

在.cshtml.cs

public void OnGet()
{

    var viewmodel = new Source
    {
        FieldName = "Test",
        OptionList = new List<SelectListItem>
        {
            new SelectListItem
            {
                Text = "Test Val1",
                Value = "1"
            },
            new SelectListItem
            {
                Text = "Test Val2",
                Value = "2"
            }
        }
    };

    ViewData["Source"] = viewmodel;
}

源类

public class Source
{
    public IEnumerable<SelectListItem> OptionList { get; set; }
    public string FieldName { get; set; }
}

局部视图

@model Source

@Html.DropDownList(Model.FieldName,
    new List<SelectListItem>(Model.OptionList),
    "Choose",
    new
    {
        @multiple = "multiple",
        @class = "input-sm",
        @size = 4
    })

测试结果

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.