简体   繁体   English

DropDownListFor 没有 List 类型

[英]DropDownListFor without a List type

I am currently working with the Salesforce API to basically pull data from Salesforce and push it back.我目前正在使用 Salesforce API 基本上从 Salesforce 中提取数据并将其推回。

There is this one section where I need to pull the Data and Deserialize it so I have model access to it.在这一节中,我需要提取数据并对其进行反序列化,以便我可以对其进行模型访问。

This certain property in the model, called Subject, is not of type list because there aren't multiple of them, BUT there are multiple Subjects from different objects coming in, and thats what I want to put in the dropdown.模型中的这个特定属性,称为主题,不是列表类型,因为它们没有多个,但是有来自不同对象的多个主题进入,这就是我想放在下拉列表中的内容。

How would I go about this?我该怎么办?

My GetAllEvents() method in my Services.我的服务中的 GetAllEvents() 方法。

        public async Task<SFEventModel> GetAllEvents()
        {
            HttpClient queryClient = new HttpClient();
            var tm = await GetToken();
            List<SelectListItem> events = new List<SelectListItem>();

            string restQuery = tm.ServiceUrl + "/services/data/v25.0/query?q=SELECT+Subject+from+Event";

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, restQuery);


            // Adding the token to the header 
            request.Headers.Add("Authorization", "Bearer " + tm.AccessToken);
            // Return JSON to the caller
            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //Call Endpoint Async
            HttpResponseMessage response = await queryClient.SendAsync(request);

            string responseString = await response.Content.ReadAsStringAsync();
            var model = JsonConvert.DeserializeObject<SFEventModel>(responseString);

            return model;
        }

My half-assed attempt of putting these inbound objects into a list我将这些入站对象放入列表的半途而废的尝试

    <div class="text-center">
        <div>
            @Html.LabelFor(x => Model.Subject)
            <div class="col-md-5">
                @Html.DropDownListFor(x => Model.Subject, new SelectList(Model.Subject))
            </div>
        </div>
    </div>

I am going to filter out all the other properties as they have no context to this question and there are lots of properties.我将过滤掉所有其他属性,因为它们没有与此问题相关的上下文,并且有很多属性。 I have the data pulling correctly from this model in my controller to actually pull "PicklistValues", but the problem is, it is considered a type.我在我的控制器中从这个模型中正确提取数据以实际提取“PicklistValues”,但问题是,它被认为是一种类型。

public partial class Field 
{
[JsonProperty("picklistValues")
public PicklistValue[] PicklistValues { get; set; }
}

PicklistValue partial class in model -->模型中的 PicklistValue 部分类 -->

public partial class PicklistValue
        {
            [JsonProperty("active")]
            public bool Active { get; set; }

            [JsonProperty("defaultValue")]
            public bool DefaultValue { get; set; }

            [JsonProperty("label")]
            public string Label { get; set; }

            [JsonProperty("validFor")]
            public object ValidFor { get; set; }

            [JsonProperty("value")]
            public string Value { get; set; }
        }

method in controller to pull these values coming in from the API call.控制器中的方法从 API 调用中提取这些值。

var allEvents = await _api.GetAllEvents();
var subjectList = allEvents.Fields.Where(x => x.Name == "Subject").Select(x => x.PicklistValues).FirstOrDefault();

EDIT: SOLVED编辑:已解决

Might have just worded the question wrong but I did it.可能只是措辞错误,但我做到了。

My Index action in my HomeController had to have this method -> ViewData["Picklist1"] = new SelectLit(await GetSubject(), "Value", "Label")我的 HomeController 中的索引操作必须有这个方法 -> ViewData["Picklist1"] = new SelectLit(await GetSubject(), "Value", "Label")

and my view had to have this <select asp-items="ViewBag.Picklist1"> </select>我的观点必须有这个<select asp-items="ViewBag.Picklist1"> </select>

for the curious, my GetSubject method was tailored to the fact that Salesforce API is terrible.出于好奇,我的 GetSubject 方法是针对 Salesforce API 很糟糕的事实量身定制的。

private async Task<List<PicklistValue>> GetSubject()
 {
    var allEvents = await _api.GetAllEvents();
    var subjectList = allEvents.Fields.Where(x => x.Name == "Subject").Select(x => 
           x.PicklistValues).FirstOrDefault().ToList();

    return subjectList;
 }

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

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