我正在尝试使用MVC6 Tag Helpers创建CountryCode和CountryName的下拉列表,以便用户在注册后可以选择他们的国家/地区。到目前为止,视图的相关部分看起来像这样
<form asp-controller="Manage" asp-action="EditCountry" asp-route-returnurl="@ViewData["ReturnUrl"]">
<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div>
<select asp-for="CountryCode" asp-items="@Model.Countries"></select>
viewmodel的相关部分看起来像这样
[Display(Name = "Country")]
public string CountryCode { get; set; }
public IEnumerable<Country> Countries { get; set; }
国家/地区看起来像这样
public partial class Country
{
[Key]
public string CountryCode { get; set; }
public string CountryName { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
控制器将国家/地区列表返回到viewmodel
var model = new IndexViewModel
{
CountryCode = user.CountryCode,
Countries =_customersContext.Countries.OrderBy(c=>c.CountryName),
};
return View(model);
}
但在视图中asp-items="@Model.Countries"
有一个波浪形的Cannot convert Country to SelectListItem
此外,我无法在表单中找到如何将CountryCode指定为要返回的属性,将CountryName指定为要显示的属性。
答案 0 :(得分:46)
我制作下拉菜单的方式有点类似,只是在我的ViewModel中,我的属性类型为SelectList
而不是IEnumerable<>
。
public class HomeViewModel
{
public string CountryCode { get; set; }
public SelectList CountryList { get; set; }
}
然后在控制器中我获取数据并将其转换为具有两个属性“Id”和“Value”的匿名列表。
反过来,我创建了一个新的SelectList()
传递匿名列表,指定dataValueField
是什么,dataTextField
是什么。
public IActionResult Index()
{
var countries = _customersContext.Countries.OrderBy(c => c.CountryName).Select(x => new { Id = x.Code, Value = x.Name });
var model = new HomeViewModel();
model.CountryList = new SelectList(countries, "Id", "Value");
return View(model);
}
最后,在视图中:
<div class="form-group">
<label asp-for="CountryCode" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="CountryCode" asp-items="@Model.CountryList"></select>
</div>
</div>
答案 1 :(得分:1)
我已经提出了另一种方法,通过扩展 Node **nodeArray;
nodeArray = malloc(10 * sizeof(Node *));
int i;
for (i = 0; i < 10; i++) {
nodeArray[i] = malloc(sizeof(Node));
}
if (userChoice == 'a')
add(&nodeArray, setNumber);
void add(Node ***nodeArray, int setNumber) {
char userString[5];
printf("Please enter some data: ");
scanf("%s", userString);
Node *head = *nodeArray[setNumber]; /* head pointer to first element of array (dummy) */
Node *newNode = malloc(sizeof(Node)); /* new node to be added to array */
strncpy(newNode->data, userString, sizeof(newNode->data)); /* copies string entered by the user to data field of new node */
newNode->next = NULL; /* initializes next field of new node to NULL */
Node *tmp = head; /* pointer to head of list */
while (!tmp->next) {
tmp->next = newNode; /* inserts new node into array */
tmp = newNode; /* points head to newly added node */
}
tmp = head; /* points tmp back to head of list */
printf("List is: ");
while (tmp->next) {
printf("%s", (tmp->data));
tmp = tmp->next;
}
}
以及一些可以使这种类型的开发更方便的属性。我们讨论了这个问题here。
它基于类<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:strokeColor="#FF000000"
android:strokeWidth="0.8"
android:strokeAlpha="0.5"
android:strokeLineJoin="round"
android:strokeLineCap="round"
android:strokeMiterLimit="1"
android:pathData="M12,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.89,2 2,2zM18,16v-5c0,-3.07 -1.64,-5.64 -4.5,-6.32L13.5,4c0,-0.83 -0.67,-1.5 -1.5,-1.5s-1.5,0.67 -1.5,1.5v0.68C7.63,5.36 6,7.92 6,11v5l-2,2v1h16v-1l-2,-2z"/>
</vector>
,它包含项目列表,分别显示文本和值字段的属性。然后在视图中一个简单的类型
<ImageView
android:id="@+id/ivNotification"
android:layout_width="@dimen/navigation_icon_size"
android:layout_height="@dimen/navigation_icon_size"
android:scaleType="fitXY"
android:src="@drawable/vector_icon_notification"/>
。
国家描述符只是GradientDrawable gdFavourite = (GradientDrawable) ivFavourite.getDrawable();
gdFavourite.setStroke(1, colorPrimary);
gdFavourite.setColor(colorPrimary);
tvFavourite.setTextColor(colorPrimary);
。这可以通过利用`nameof-operator