首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SelectBox不向我显示从ViewData字典传输的数据

SelectBox不向我显示从ViewData字典传输的数据
EN

Stack Overflow用户
提问于 2021-11-10 22:24:54
回答 1查看 38关注 0票数 0

我试图在两个独立的下拉列表中显示从ViewData["ProductGroupId"]ViewData["ProductSubGroupId"]收到的数据。在调试时,一切正常,我可以看到我的数据是从数据库中正确获取的,但是当我运行应用程序时,我看到我的下拉列表是空的!就像贝洛一样:

我在controller中的代码

代码语言:javascript
复制
[Microsoft.AspNetCore.Mvc.HttpGet]
public IActionResult Create()
{
    var groups = _productServices.GetGroupsListItem();
    var subGroups = _productServices.GetSubGroups_ByGroupId_ListItem(int.Parse(groups.First().Value));
    ViewData["ProductGroupId"] = new SelectList(groups.ToList(), "Value", "Text");
    ViewData["ProductSubGroupId"] = new SelectList(subGroups.ToList(), "Value", "Text");

    return View();
}

我的View Razore是:

代码语言:javascript
复制
@model Keyhanatr.Data.Domain.Products.Product
@{
    ViewData["Title"] = "ایجاد محصول";
    Layout = "~/Areas/Admin/Views/Shared/_AdminLayout.cshtml";
}
<form asp-action="Create" method="post" enctype="multipart/form-data">
    <section class="col-lg-12 pt-lg-4 pb-4 mb-3">
        <div class="pt-2 px-4 ps-lg-0 pe-xl-5">

            -->
            Title
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="d-sm-flex flex-wrap justify-content-between align-items-center pb-2">
                <h2 class="h3 py-2 me-2 text-center text-sm-start">درج محصول جدید</h2>

            </div>

            <div class="form-group">
                <label asp-for="@Model.ProductGroupId"></label>
                <select asp-for="@Model.ProductGroupId" class="form-select me-2" asp-items="@(ViewBag.ProductGroupId as SelectList)"></select>
            </div>
            <div class="form-group">
                <label asp-for="@Model.ProductSubGroupId"></label>
                <select asp-for="@Model.ProductSubGroupId" class="form-select me-2" asp-items="@(ViewData["ProductSubGroupId"] as SelectList)"> </select>
            </div>

            <div class="mb-3 pb-2">
                <label class="form-label" for="unp-product-name">نام محصول</label>
                <input class="form-control" asp-for="@Model.ProductTitle" id="unp-product-name">
                <div class="form-text">حداکثر 100 حرف هیچ L یا شکلک مجاز نیست.</div>
            </div>
            <div class="file-drop-area mb-3">
                <div class="file-drop-icon ci-cloud-upload"></div><span class="file-drop-message">برای بارگذاری تصویر صفحه از محصول ، آن را بکشید و رها کنید</span>
                <input class="file-drop-input" type="file" name="imgUp">
                <button class="file-drop-btn btn btn-primary btn-sm mb-2" type="button">یا پرونده را انتخاب کنید</button>
                <div class="form-text">1000 x 800px اندازه ایده آل برای نمایشگرهای با وضوح بالا</div>
            </div>
            @if (ViewBag.IsNullImg != null)
            {
                <div class="form-text text-danger"><span>لطفا یک تصویر را برای محصول انتخاب کنید</span></div>
            }
            <div class="mb-3 py-2">
                <label class="form-label" for="unp-product-description">توضیحات محصول</label>
                <textarea asp-for="@Model.Description" class="form-control" rows="6" id="unp-product-description"></textarea>
                <div class="bg-secondary p-3 fs-ms rounded-bottom"><span class="d-inline-block fw-medium me-2 my-1">پشتیبانی می کند:</span><em class="d-inline-block border-end pe-2 me-2 my-1">*کج*</em><strong class="d-inline-block border-end pe-2 me-2 my-1">**برجسته**</strong><span class="d-inline-block border-end pe-2 me-2 my-1">- لیست</span><span class="d-inline-block border-end pe-2 me-2 my-1">##سربرگ##</span><span class="d-inline-block">--- افقی</span></div>
            </div>
            <div class="row">
                <div class="col-sm-12 mb-6">
                    <label class="form-label" for="unp-standard-price">قیمت استاندارد</label>
                    <div class="input-group">
                        <input class="form-control" type="text" id="unp-standard-price">
                        <span asp-validation-for="Price" class="text-danger"></span>
                    </div>
                    <div class="form-text">قیمت محصول بر حسب تومان اعمال خواهد شد.</div>
                </div>

            </div>
            <div class="mb-3 py-2">
                <label class="form-label" for="unp-product-tags">برچسب های محصول</label>
                <textarea placeholder="عطرمردانه-جدید-اسپرت" class="form-control" rows="4" id="unp-product-tags"></textarea>
                <div class="form-text">حداکثر 10 کلمه کلیدی که مورد شما را توصیف می کنند. همه برچسب ها باید با حروف کوچک و با خط فاصله یا همان (-) از هم جدا شوند.</div>
            </div>
            <button class="btn btn-primary d-block w-100" type="submit"><i class="ci-cloud-upload fs-lg me-2"></i>آپلود محصول</button>
        </div>
    </section>
</form>

有人能帮上忙吗?

EN

回答 1

Stack Overflow用户

发布于 2021-11-11 01:54:00

对于public SelectList(IEnumerable items, string dataValueField, string dataTextField);,第一个参数是源,第二个参数是select中的每个选项值,第三个参数是每个选项在html中显示文本。第二个和第三个参数应该是源中的属性。

因此,您需要确保TextValuegroupssubGroups中的属性。

下面是一个简单的演示:

型号:

代码语言:javascript
复制
public class Product
{
    public int ProductGroupId { get; set; }
    public int ProductSubGroupId { get; set; }
    public string Price { get; set; }
    public string ProductTitle { get; set; }
    public string Description { get; set; }
}
public class Group
{
    public string Id { get; set; }
    public string Name { get; set; }
}
public class SubGroup
{
    public string Id { get; set; }
    public string SubName { get; set; }
}

查看:

代码语言:javascript
复制
@model Product
@{
    ViewData["Title"] = "ایجاد محصول";
}
<form asp-action="Create" method="post" enctype="multipart/form-data">
    <section class="col-lg-12 pt-lg-4 pb-4 mb-3">
        <div class="pt-2 px-4 ps-lg-0 pe-xl-5">    
            -->
            Title
            <div asp-validation-summary="All" class="text-danger"></div>
            <div class="d-sm-flex flex-wrap justify-content-between align-items-center pb-2">
                <h2 class="h3 py-2 me-2 text-center text-sm-start">درج محصول جدید</h2>

            </div>

            <div class="form-group">
                <label asp-for="@Model.ProductGroupId"></label>
                <select asp-for="@Model.ProductGroupId" class="form-select me-2" asp-items="@(ViewBag.ProductGroupId as SelectList)"></select>
            </div>
            <div class="form-group">
                <label asp-for="@Model.ProductSubGroupId"></label>
                <select asp-for="@Model.ProductSubGroupId" class="form-select me-2" asp-items="@(ViewData["ProductSubGroupId"] as SelectList)"> </select>
            </div>

            <div class="mb-3 pb-2">
                <label class="form-label" for="unp-product-name">نام محصول</label>
                <input class="form-control" asp-for="@Model.ProductTitle" id="unp-product-name">
                <div class="form-text">حداکثر 100 حرف هیچ L یا شکلک مجاز نیست.</div>
            </div>
            <div class="file-drop-area mb-3">
                <div class="file-drop-icon ci-cloud-upload"></div><span class="file-drop-message">برای بارگذاری تصویر صفحه از محصول ، آن را بکشید و رها کنید</span>
                <input class="file-drop-input" type="file" name="imgUp">
                <button class="file-drop-btn btn btn-primary btn-sm mb-2" type="button">یا پرونده را انتخاب کنید</button>
                <div class="form-text">1000 x 800px اندازه ایده آل برای نمایشگرهای با وضوح بالا</div>
            </div>
           
            <div class="mb-3 py-2">
                <label class="form-label" for="unp-product-description">توضیحات محصول</label>
                <textarea asp-for="@Model.Description" class="form-control" rows="6" id="unp-product-description"></textarea>
                <div class="bg-secondary p-3 fs-ms rounded-bottom"><span class="d-inline-block fw-medium me-2 my-1">پشتیبانی می کند:</span><em class="d-inline-block border-end pe-2 me-2 my-1">*کج*</em><strong class="d-inline-block border-end pe-2 me-2 my-1">**برجسته**</strong><span class="d-inline-block border-end pe-2 me-2 my-1">- لیست</span><span class="d-inline-block border-end pe-2 me-2 my-1">##سربرگ##</span><span class="d-inline-block">--- افقی</span></div>
            </div>
            <div class="row">
                <div class="col-sm-12 mb-6">
                    <label class="form-label" for="unp-standard-price">قیمت استاندارد</label>
                    <div class="input-group">
                        <input class="form-control" type="text" id="unp-standard-price">
                        <span asp-validation-for="Price" class="text-danger"></span>
                    </div>
                    <div class="form-text">قیمت محصول بر حسب تومان اعمال خواهد شد.</div>
                </div>

            </div>
            <div class="mb-3 py-2">
                <label class="form-label" for="unp-product-tags">برچسب های محصول</label>
                <textarea placeholder="عطرمردانه-جدید-اسپرت" class="form-control" rows="4" id="unp-product-tags"></textarea>
                <div class="form-text">حداکثر 10 کلمه کلیدی که مورد شما را توصیف می کنند. همه برچسب ها باید با حروف کوچک و با خط فاصله یا همان (-) از هم جدا شوند.</div>
            </div>
            <button class="btn btn-primary d-block w-100" type="submit"><i class="ci-cloud-upload fs-lg me-2"></i>آپلود محصول</button>
        </div>
    </section>
</form>

控制器:

代码语言:javascript
复制
public IActionResult Create()
{
    //var groups = _productServices.GetGroupsListItem();
    //var subGroups = _productServices.GetSubGroups_ByGroupId_ListItem(int.Parse(groups.First().Value));
    //for easy testing, I just hard-coded here......
    var groups = new List<Group>()
    {
        new Group(){Id="1",Name="aa"},
        new Group(){Id="2",Name="bb"},
        new Group(){Id="3",Name="cc"}
    };
    var subGroups = new List<SubGroup>()
    {
        new SubGroup(){Id="1",SubName="SubA"},
        new SubGroup(){Id="2",SubName="SubB"},
        new SubGroup(){Id="3",SubName="SubC"}
    };
    ViewData["ProductGroupId"] = new SelectList(groups.ToList(), "Id", "Name");
    ViewData["ProductSubGroupId"] = new SelectList(subGroups.ToList(), "Id", "SubName");
    return View();

}

结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69920935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档