我有产品表,主键(数据类型为nvarchar(50)),如
public List<string> GetProductsList()
{
List<string> productsList = new List<string>();
using (Entities database = new Entities ())
{
productsList = (from product
in database.Products
select product.ID).Take(10).ToList();
}
return productsList;
}
在Model i中获取产品列表
@Html.DropDownListFor(model => model.Product, new SelectList(Model.ProductsList), "-- Product --", new { @class = "form-control" } )
在View I中显示带产品的DropDownList
100531 00001
但DropDownList中的项目显示为
100531 00001
产品ID也是Orders表中的外键。但是我无法添加带有产品ID的新订单
Bundle bundle = new Bundle();
bundle.putStringArrayList("valuesArray", namesArray);
namesFragment myFragment = new namesFragment();
myFragment.setArguments(bundle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, myFragment);
fragmentTransaction.commit();
我的问题是:我可以在应用程序中更改某些内容以显示数据,就像它在数据库中一样,或者我必须在数据库中更改某些内容吗?
更新
在此处找到解决方案How to retain spaces in DropDownList - ASP.net MVC Razor views
答案 0 :(得分:0)
You can use this Replace(" ", " ")
method to preserve whitespaces in dropdownlistfor like following code.
Reference: http://www.dennisonpro.info/preserving-whitespace-in-dropdownlist-items-with-c-mvc-3/
public List<string> GetProductsList()
{
List<string> productsList = new List<string>();
using (Entities database = new Entities ())
{
productsList = (from product
in database.Products
select product.ID).Take(10).ToList();
}
productsList = productsList.Select(str=>str.Replace(" ", " "));
return productsList;
}
答案 1 :(得分:-1)
如果要将某些内容绑定到下拉列表,我建议使用键值对列表。目前,您返回一个包含产品ID的字符串列表,但您应该返回产品ID和产品ID或产品ID和产品名称的键值对。这样你就可以将下拉列表绑定为
$scope.pets = [];
$http.get(baseApiUrl + '/pets').
success(function (data) {
console.log("Success : " + data);
$scope.pets = data;
}).error(function () {
console.log(data);
});
$scope.UpdateData = function (pet) {
$http({
method: 'PUT',
url: baseApiUrl + '/pets/' + pet.id,
data: {
id: pet.id,
name: pet.updateName,
age: pet.updateAge,
owner: pet.updateOwner,
}
}).then(function successCallback(response) {
console.log(response);
}, function errorCallback(response) {
console.log(response);
});
};
而不是您当前的实施
{{1}}
您当前实现的问题是您的产品ID中有多个空格,但html只允许一个空格字符,除非是这样。但是在数据库中你有空白字符而不是&amp; nbsp;因此,在发布数据时,您只能得到1个与您的数据库记录不匹配的空格。因此,我建议您返回列表的键值对。